条件变量pthread_cond_t使用示例

46 篇文章 1 订阅
3 篇文章 0 订阅

示例代码:

[cpp]   view plain copy
  1. #include <pthread.h>  
  2. #include <stdio.h>  
  3. #include <stdlib.h>  
  4. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;/*初始化互斥锁*/  
  5. pthread_cond_t cond = PTHREAD_COND_INITIALIZER;/*初始化条件变量*/  
  6. void *thread1(void *);  
  7. void *thread2(void *);  
  8. int i=1;  
  9. int main(void)  
  10. {  
  11.     pthread_t t_a;  
  12.     pthread_t t_b;  
  13.     pthread_create(&t_a,NULL,thread1,(void *)NULL);/*创建进程t_a*/  
  14.     pthread_create(&t_b,NULL,thread2,(void *)NULL); /*创建进程t_b*/  
  15.     pthread_join(t_a, NULL);/*等待进程t_a结束*/  
  16.     pthread_join(t_b, NULL);/*等待进程t_b结束*/  
  17.     pthread_mutex_destroy(&mutex);  
  18.     pthread_cond_destroy(&cond);  
  19.     exit(0);  
  20. }  
  21. void *thread1(void *junk)  
  22. {  
  23.     for(i=1;i<=6;i++)  
  24.     {  
  25.         pthread_mutex_lock(&mutex);/*锁住互斥量*/  
  26.         printf("thread1: lock %d/n", __LINE__);  
  27.         if(i%3==0){  
  28.             printf("thread1:signal 1  %d/n", __LINE__);  
  29.             pthread_cond_signal(&cond);/*条件改变,发送信号,通知t_b进程*/  
  30.             printf("thread1:signal 2  %d/n", __LINE__);  
  31.             sleep(1);  
  32.         }  
  33.         pthread_mutex_unlock(&mutex);/*解锁互斥量*/  
  34.         printf("thread1: unlock %d/n/n", __LINE__);  
  35.         sleep(1);  
  36.     }  
  37. }  
  38. void *thread2(void *junk)  
  39. {  
  40.     while(i<6)  
  41.     {  
  42.         pthread_mutex_lock(&mutex);  
  43.         printf("thread2: lock %d/n", __LINE__);  
  44.         if(i%3!=0){  
  45.             printf("thread2: wait 1  %d/n", __LINE__);  
  46.             pthread_cond_wait(&cond,&mutex);/*解锁mutex,并等待cond改变*/  
  47.             printf("thread2: wait 2  %d/n", __LINE__);  
  48.         }  
  49.         pthread_mutex_unlock(&mutex);  
  50.         printf("thread2: unlock %d/n/n", __LINE__);  
  51.         sleep(1);  
  52.     }  
  53. }  

编译:

[X61@horizon threads]$ gcc thread_cond.c -lpthread -o tcd

以下是程序运行结果:

[X61@horizon threads]$ ./tcd 
thread1: lock 30
thread1: unlock 40

thread2: lock 52
thread2: wait 1 55
thread1: lock 30
thread1: unlock 40

thread1: lock 30
thread1:signal 1 33
thread1:signal 2 35
thread1: unlock 40

thread2: wait 2 57
thread2: unlock 61

thread1: lock 30
thread1: unlock 40

thread2: lock 52
thread2: wait 1 55
thread1: lock 30
thread1: unlock 40

thread1: lock 30
thread1:signal 1 33
thread1:signal 2 35
thread1: unlock 40

thread2: wait 2 57
thread2: unlock 61

这里的两个关键函数就在pthread_cond_wait和pthread_cond_signal函数

本例中:
线程一先执行,获得mutex锁,打印,然后释放mutex锁,然后阻塞自己1秒。
线程二此时和线程一应该是并发的执行
 ,这里是一个要点,为什么说是线程此时是并发的执行,因为此时不做任何干涉的话,是没有办法确定是线程一先获得执行,还是线程二先获得执行,到底那个线程先获得执行,取决于操作系统的调度,想刻意的让线程2先执行,可以让线程2一出来,先sleep一秒。
这里并发执行的情况是:

线程一先进入循环,然后获得锁,此时估计线程二执行,阻塞在

pthread_mutex_lock(&mutex);

这行语句中,直到线程1释放mutex锁

pthread_mutex_unlock(&mutex); /*解锁互斥量*/


然后线程二得已执行,获取mutex锁,满足if条件,到

pthread_cond_wait (&cond,&mutex);/*等待*/

这一行,这里的线程二阻塞,不仅仅是等待cond变量发生改变,同时释放mutex锁 ,因为当时看书没有注意,所以这里卡了很久。

mutex锁释放后,线程1终于获得了mutex锁,得已继续运行,当线程1的if(i%3==0)的条件满足后,通过pthread_cond_signal发送信号,告诉等待cond的变量的线程(这个情景中是线程二),cond条件变量已经发生了改变。

不过此时线程二并没有立即得到运行 ,因为线程二还在等待mutex锁的释放,所以线程一继续往下走,直到线程一释放mutex锁,线程二才能停止等待,打印语句,然后往下走,通过pthread_mutex_unlock(&mutex)释放mutex锁,进入下一个循环。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值