- 1、本文档共352页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
大学课件物联网与嵌的入式系统开发--第6章_嵌入式Linux应用开发
6.9 Linux应用程序编程 6.9.10 线程(续) 10)简单多线程示例 下面给出一个非常简单的多线程示例,代码比较容易,这里不再详细讲解。这里要注意的是,使用gcc编译线程程序时需自己链接pthread库,Linux默认是不自动链接该库的。假设下面的程序的文件名为thread.c,则编译该程序时应使用命令“gcc thread.c -o thread -lpthread”。 #define THREAD_NUM 5 void *start_routine(void *arg) { printf(arg = %d\n, (int)arg); pthread_exit(NULL); } 6.9 Linux应用程序编程 6.9.10 线程(续) 10)简单多线程示例 int main() { pthread_t thread[THREAD_NUM]; int i, j; for (i=0; iTHREAD_NUM; i++) { if(pthread_create(thread[i], NULL, start_routine, (void *)i)!=0) { printf(Create thread %d failed.\n, i); break; } } 6.9 Linux应用程序编程 6.9.10 线程(续) 10)简单多线程示例 for (j=0; ji; j++) { if (pthread_join(thread[j], NULL) != 0) { printf(Thread %d join failed.\n, j); continue; } } } 6.9 Linux应用程序编程 6.9.10 线程(续) 11)使用互斥锁示例 #define THREAD_NUM 5 int n = 0; pthread_mutex_t mutex; void *start_routine(void *arg) { if(pthread_mutex_lock(mutex) != 0) { printf(Thread %d lock failed.\n, (int)arg); pthread_exit(NULL); } printf(n = %d\n, n++); if(pthread_mutex_unlock(mutex) != 0) printf(Thread %d unlock failed.\n,(int)arg); pthread_exit(NULL); } 6.9 Linux应用程序编程 6.9.10 线程(续) 11)使用互斥锁示例 int main() { pthread_t thread[THREAD_NUM]; int i, j; pthread_mutex_init(mutex, NULL); for (i=0; iTHREAD_NUM; i++) { if(pthread_create(thread[i], NULL, start_routine, (void *)i) !=0) { printf(Create thread %d failed.\n, i); break; } } 6.9 Linux应用程序编程 6.9.10 线程(续) 11)使用互斥锁示例 for (j=0; ji; j++) { if (pthread_join(thread[j], NULL) != 0) { printf(Thread %d join failed.\n, j); continue; } } pthread_mutex_destroy(mutex); } 6.9 Linux应用程序编程 6.9.10 线程(续) 12)使用信号量示例 #define THREAD_NUM 5 int n = 0; sem_t sem; void *start_routine(void *arg) { if(sem_wait(sem) 0) { printf(Thread %d wait failed.\n, (int)arg); pthread_exit(NULL); } printf(n = %d\n, n++); if(sem_post(sem) 0) { printf(Thr
您可能关注的文档
最近下载
- (高清版)DB33∕T 2363.3-2021 市域(郊)铁路工程质量验收规范 第3部分:隧道工程.pdf VIP
- SH_T 3501-2021 石油化工有毒、可燃介质钢制管道工程施工及验收规范.docx VIP
- 企业房屋买卖合同5篇.docx VIP
- 小数与小数的乘法.ppt VIP
- 社会山东苑小区热力站工艺设备采购及安装工程施工组织设计-技术标.pdf VIP
- 医疗器械生产企业供应商管理体系审核指南.docx VIP
- 纸箱采购投标方案(技术方案).doc
- 标准图集-20S515-钢筋混凝土及砖砌排水检查井.pdf VIP
- SH3501-2021石油化工有毒、可燃介质钢制管道工程施工及验收规范(新旧版本对照)(1).pptx VIP
- DB11_T537-2019:墙体内保温施工技术规程胶粉聚苯颗粒保温浆料做法和增强粉刷石膏聚苯板做法.pdf VIP
文档评论(0)