多线程互斥和同步
来源:互联网 发布:水镜先生知乎 编辑:程序博客网 时间:2024/06/05 14:06
一、线程互斥
1.线程概念
线程就是“轻量级”的进程。
线程与创建它的进程共享代码段,数据段。
线程拥有自己独立的栈。
2.函数学习
创建线程:int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
头文件: 编译时需要连接-phtread
成功:0 失败:返回错误数字
*thread:函数创建的线程id
*attr:创建线程的属性,NULL
void *(*start_routine)(void *):运行的函数
*arg:函数的参数
等待函数结束:pthread_join
函数原型:int pthread_join(pthread_t thread, void **retval);
头文件: 编译时需要连接-phtread
成功:0 失败:返回错误数字
thread:等待结束的线程id
**retval:保存线程退出时的状态,一般为NULL
退出线程:pthread_exit
函数原型:void pthread_exit(void *retval);
头文件: 编译时需要连接-phtread
返回值为空
*retval:保存线程退出时的状态
3.线程互斥
在实际应用中,多个线程往往会访问同一数据或资源,为避免线程之间相互影响,需要引入线程互斥机制,而互斥锁(mutex)互斥机制中的一种。
初始化mutex: int pthread_mutex_init(pthread_mutex_t *retrict mutex, const pthread_mutexattr_t *restrict attr);
头文件:
成功:0 失败:错误数字
mutex:互斥锁的地址
attr:互斥锁的属性
互斥锁上锁:int pthread_mutex_lock(pthread_mutex_t *mutex);
头文件:
成功:0 失败:错误数字
mutex:互斥锁的地址
互斥锁解锁:int pthread_mutex_unlock(pthread_mutex_t *mutex);
头文件:
成功:0 失败:错误数字
mutex:互斥锁的地址
4 编写代码
thread.c
二、线程同步
1.线程同步
多个线程按照规定的顺序来执行,即为线程同步。
2.条件变量
初始化
sync.c:
1.线程概念
线程就是“轻量级”的进程。
线程与创建它的进程共享代码段,数据段。
线程拥有自己独立的栈。
2.函数学习
创建线程:int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
头文件: 编译时需要连接-phtread
成功:0 失败:返回错误数字
*thread:函数创建的线程id
*attr:创建线程的属性,NULL
void *(*start_routine)(void *):运行的函数
*arg:函数的参数
等待函数结束:pthread_join
函数原型:int pthread_join(pthread_t thread, void **retval);
头文件: 编译时需要连接-phtread
成功:0 失败:返回错误数字
thread:等待结束的线程id
**retval:保存线程退出时的状态,一般为NULL
退出线程:pthread_exit
函数原型:void pthread_exit(void *retval);
头文件: 编译时需要连接-phtread
返回值为空
*retval:保存线程退出时的状态
3.线程互斥
在实际应用中,多个线程往往会访问同一数据或资源,为避免线程之间相互影响,需要引入线程互斥机制,而互斥锁(mutex)互斥机制中的一种。
初始化mutex: int pthread_mutex_init(pthread_mutex_t *retrict mutex, const pthread_mutexattr_t *restrict attr);
头文件:
成功:0 失败:错误数字
mutex:互斥锁的地址
attr:互斥锁的属性
互斥锁上锁:int pthread_mutex_lock(pthread_mutex_t *mutex);
头文件:
成功:0 失败:错误数字
mutex:互斥锁的地址
互斥锁解锁:int pthread_mutex_unlock(pthread_mutex_t *mutex);
头文件:
成功:0 失败:错误数字
mutex:互斥锁的地址
4 编写代码
thread.c
- #include <pthread.h>
- #include <stdio.h>
- pthread_t thread[2];
- int number = 0;
- pthread_mutex_t mutex;
- void *worker1()
- {
- int i = 0;
- printf("I am worker1.\n");
- for(i=0;i<10;i++)
- {
- pthread_mutex_lock(&mutex);
- number++;
- pthread_mutex_unlock(&mutex);
- printf("worker1 number is %d\n", number);
- sleep(1);
- }
- pthread_exit(NULL);
- }
- void *worker2()
- {
- int i = 0;
- printf("I am worker2.\n");
- for(i=0;i<10;i++)
- {
- pthread_mutex_lock(&mutex);
- number++;
- pthread_mutex_unlock(&mutex);
- printf("worker2 number is %d\n", number);
- sleep(1);
- }
- pthread_exit(NULL);
- }
- void main()
- {
- pthread_mutex_init(&mutex, NULL);
- /*创建工人1线程*/
- pthread_create(&thread[0], NULL, worker1, NULL);
- /*创建工人2线程*/
- pthread_create(&thread[1], NULL, worker2, NULL);
- /*等待工人1线程结束*/
- pthread_join(thread[0], NULL);
- /*等待工人2线程结束*/
- pthread_join(thread[1], NULL);
- return;
- }
1.线程同步
多个线程按照规定的顺序来执行,即为线程同步。
2.条件变量
初始化
pthread_cond_t cond_ready=PTHREAD_COND_INITIALIZER;
等待条件成熟
pthread_cond_wait(&cond_ready, &mut);
设置条件成熟
pthread_cond_signal(&cond_ready);
sync.c:
- #include <sys/types.h>
- #include <pthread.h>
- int number = 0;
- pthread_t pthread[2];
- pthread_mutex_t mut;
- pthread_cond_t cond_ready = PTHREAD_COND_INITIALIZER;
- void studentA()
- {
- int i;
- /*扫5次地*/
- for(i=0;i<5;i++)
- {
- pthread_mutex_lock(&mut);
- number++;
- if(number >=5)
- {
- printf("student A has funished his work!\n");
- /*通知B同学*/
- pthread_cond_signal(&cond_ready);
- }
- pthread_mutex_unlock(&mut);
- /*休息1秒钟*/
- sleep(1);
- }
- /*退出*/
- pthread_exit(NULL);
- }
- void studentB()
- {
- pthread_mutex_lock(&mut);
- if(number < 5) /*判断A是否已经扫完5次地*/
- pthread_cond_wait(&cond_ready, &mut);
- /*拖地*/
- number = 0;
- printf("sudent B has finished his work.\n");
- /*退出*/
- pthread_exit(NULL);
- }
- int main()
- {
- pthread_mutex_init(&mut, NULL);
- /*创建A同学线程*/
- pthread_create(&pthread[0], NULL, studentA, NULL);
- /*创建B同学线程*/
- pthread_create(&pthread[1], NULL, studentB, NULL);
- /*等待A同学线程结束*/
- pthread_join(pthread[0], NULL);
- /*等待B同学线程结束*/
- pthread_join(pthread[1], NULL);
- }
0
上一篇:消息队列编程
下一篇:Linux网络编程协议
相关热门文章
- test123
- 编写安全代码——小心有符号数...
- 使用openssl api进行加密解密...
- 一段自己打印自己的c程序...
- 彻底搞定C语言指针详解-完整版...
给主人留下些什么吧!~~
评论热议
阅读全文
0 0
- 多线程互斥和同步
- windows多线程同步和互斥关系
- 多线程的同步和互斥简介
- 多线程的同步和互斥
- 多线程理解--互斥和同步
- 多线程的同步和互斥
- 多线程的互斥和同步
- 15-多线程互斥和同步
- 多线程互斥同步
- 多线程互斥同步
- 多线程同步互斥
- 多线程同步与互斥
- 多线程同步与互斥
- 多线程同步与互斥
- 多线程同步与互斥
- 多线程同步与互斥
- MFC多线程同步互斥
- 多线程互斥与同步
- linux应用编程学习(6)信号
- 通过自定义属性添加样式(css+js,动态判断背景颜色)
- 信号量的互斥与同步
- 共享内存通信
- 消息队列编程
- 多线程互斥和同步
- Linux网络编程协议
- TCP/UDP socket编程学习
- vim多行增加缩进
- yii2 邮件发送教程
- 网络并发服务器设计
- 守护进程
- Spring MVC 处理请求流程
- Python build finished, but the necessary bits to build these modules were
原创粉丝点击
热门IT博客
热门问题
老师的惩罚
人脸识别
我在镇武司摸鱼那些年
重生之率土为王
我在大康的咸鱼生活
盘龙之生命进化
天生仙种
凡人之先天五行
春回大明朝
姑娘不必设防,我是瞎子
法拉利550 maranello
法拉利 新款
f12法拉利价格
法拉利姐
法拉利355
法拉利f90
法拉利f430二手
法拉利五金
法拉利f400
法拉利458出租
法拉利f12 lp700
租赁法拉利
法拉利599报价
法拉利赛车手
法拉利 250
粉红法拉利
f12法拉利报价
婚车法拉利多少钱
租车 法拉利
法拉利200
法拉利f458敞篷
二手法拉利跑车价格
法拉利租车一天多少钱
法拉利用什么机油
法拉利平台
法拉利430价格
一辆法拉利跑车多少钱
法拉利零件
法拉利 跑车
法拉利服装专卖店
法拉利收购
法拉利berlinetta
白色法拉利价格
法拉利458现车
法拉利418
法拉利改装
法拉利f11
法拉利408
法拉利f430黑色
黑色法拉利f430
法拉利575m多少钱