- 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
您可能关注的文档
最近下载
- 《华为销售法》读书笔记.pptx VIP
- 大班语言《落叶》课件.pptx VIP
- (高清版)B 9683-1988 复合食品包装袋卫生标准.pdf VIP
- 2025年大学毛概期末考试试题库及答案.docx VIP
- 中建八局二公司绩效管理办法.doc VIP
- Unit 1 You and Me Section A How do you greet people 课件 人教版(2024)英语七年级上册.ppt VIP
- (高清版)D-Z-T 0449-2023 地质灾害气象风险预警规范.pdf VIP
- 2025江西吉安市青原区教育体育局面向社会招聘8人模拟试卷含答案详解.docx VIP
- DB11_T 1234-2022 生活垃圾焚烧处理能源消耗限额.docx VIP
- GB50173-2014 电气装置安装工程 66kV及以下架空电力线路施工及验收规范.docx VIP
文档评论(0)