Linux编程编程实战(三)
来源:互联网 发布:ubuntu安装maven 编辑:程序博客网 时间:2024/06/16 12:25
Linux编程———线程相关
创建函数pthread_create注意事项
错误示例:res = pthread_create(&tid[i],NULL,(void*)thread_func(),NULL);
亦或:res = pthread_create(&tid[i],NULL,(void*)thread_func(argument0,...),NULL);
亦或:res = pthread_create(&tid[i],NULL,(void*)(thread_func(argument0,...)),NULL);
任何在函数名称后紧着括号的情况,函数都将被执行,函数名自然也不会被作为地址传参,也就是说,实际上线程并未创建,尤其实在thread_func()是个死循环的情况下,会给人一种线程开始运行后main线程就停止了错觉,实则是卡死在了thread_func()的死循环中。
哲学家进餐问题
以哲学家进餐问题为示例,实践线程的基础使用
下面贴出代码:
#include<stdio.h>#include<time.h>#include<pthread.h>#include<unistd.h>#include<stdlib.h>#include<sched.h>//全局变量pthread_mutex_t *mutex;pthread_t *tid;int PhilosopherNum = 0; //总数量int PhilosopherNumber = 0;void philosopher(void);void philosopher(void){ int state = 1; int Philosophernum = PhilosopherNumber++; srand(time(0)); printf("哲学家%d开始思考了\n",Philosophernum+1); while(1){ switch(state){ case 0: sleep(rand()%10); state = 1; printf("哲学家%d饿了\n",Philosophernum+1); break; case 1: if(pthread_mutex_trylock(&mutex[Philosophernum]) != 0){ continue; } else{ if(pthread_mutex_trylock(&mutex[(Philosophernum+1)%PhilosopherNum]) != 0){ pthread_mutex_unlock(&mutex[Philosophernum]); } else{ state = 2; printf("哲学家%d开始进餐\n",Philosophernum+1); } } break; case 2: sleep(rand()%10); state = 0; printf("哲学家%d进餐完毕,开始思考了\n",Philosophernum+1); pthread_mutex_unlock(&mutex[Philosophernum]); pthread_mutex_unlock(&mutex[(Philosophernum+1)%PhilosopherNum]); break; default: printf("错误状态!!!\n"); exit(0); } }}int main(){ int i; pthread_attr_t attr; struct sched_param Param; pthread_attr_init(&attr); pthread_attr_getschedparam(&attr,&Param); Param.sched_priority = 20; pthread_attr_setschedparam(&attr,&Param); printf("输入哲学家数量:"); scanf("%d",&PhilosopherNum); if(PhilosopherNum < 2) PhilosopherNum = 5; mutex = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t)*PhilosopherNum); tid = (pthread_t *)malloc(sizeof(pthread_t)*PhilosopherNum); int res; for(i = 0;i < PhilosopherNum;i++){ res = pthread_mutex_init(&mutex[i],NULL); if(res!=0){ printf("互斥量初始化失败!!!\n"); exit(0); } } for(i = 0;i < PhilosopherNum;i++){ printf("%d\n",i); Param.sched_priority = 20 + i; pthread_attr_setschedparam(&attr,&Param); res = pthread_create(&tid[i],NULL,(void*)philosopher,NULL); if(res!=0){ printf("线程初始化失败!!!\n"); exit(0); } } rewind(stdin); char key = 0; while(key != 'q') key = getchar(); for(i = 0;i < PhilosopherNum;i++){ pthread_cancel(&tid[i]); } return 0;}
阅读全文
0 0
- Linux编程编程实战(三)
- 实战Linux Bluetooth编程(三) HCI层编程
- 实战Linux Bluetooth编程(三) HCI层编程
- 实战Linux Bluetooth编程(三) HCI层编程
- 实战Linux Bluetooth编程(三) HCI层编程
- 实战Linux Bluetooth编程(三) HCI层编程
- 实战Linux Bluetooth编程(三) HCI层编程
- 实战Linux Bluetooth编程(三) HCI层编程
- 实战Linux Bluetooth编程(三) HCI层编程
- 实战Linux Bluetooth编程(三) HCI层编程
- 实战Linux Bluetooth编程(三) HCI层编程
- 实战Linux Bluetooth编程(三) HCI层编程
- 实战Linux Bluetooth编程(三) HCI层编程
- Linux下的C编程实战(三)
- Linux Shell编程进阶案例实战(三)
- linux c 编程实战:文件操作总结(三)
- linux c 编程实战:进程控制总结(三)
- Linux下的C编程实战(三)----进程控制与进程通信编程
- SSH免密码登录
- Android中WebView加载H5页面JS不执行的问题
- loj #6004. 「网络流 24 题」圆桌聚餐(最大流)
- 输出100个100以内的随机数(C++)
- DDL、DML和DCL的区别与理解
- Linux编程编程实战(三)
- Python 初探2
- Oracle存储过程基本语法介绍
- 记录Android 版本小于19的Webview和js交互的一个坑!
- 预解析
- typedef定义数据类型
- [改善Java代码]不要在构造函数中抛出异常
- 关于STM32的JTAG复用
- 作用域和作用域链