pthread_cond_wait/pthread_cond_signal/pthread_cond_broadcast

13 篇文章 0 订阅
3 篇文章 0 订阅
       pthread_cond_wait() 用于阻塞当前线程,等待别的线程使用pthread_cond_signal()或pthread_cond_broadcast来唤醒它。 pthread_cond_wait() 必须与pthread_mutex 配套使用。pthread_cond_wait()函数一进入wait状态就会自动release mutex( 解释一下:调用pthread_cond_wait的线程,在挂起之前必须unlock入参pthread_mutex,否者一单线程挂起,它就不再占用CPU,当然unlock不了pthread_mutex,但是为了关闭时间窗,调用pthread_cond_wait的线程会先禁止CPU抢占,然后unlock mutex,然后挂起自己 )。当其他线程通过pthread_cond_signal()或pthread_cond_broadcast,把该线程唤醒,使pthread_cond_wait()通过(返回)时,该线程又自动获得该mutex。
  pthread_cond_signal函数的作用是发送一个信号给另外一个正在处于阻塞等待状态的线程,使其脱离阻塞状态,继续执行.如果没有线程处在阻塞等待状态,pthread_cond_signal也会成功返回。
  使用pthread_cond_signal一般不会有“惊群现象”产生,他最多只给一个线程发信号。假如有多个线程正在阻塞等待着这个条件变量的话,那么是根据各等待线程优先级的高低确定哪个线程接收到信号开始继续执行。如果各线程优先级相同,则根据等待时间的长短来确定哪个线程获得信号。但无论如何一个pthread_cond_signal调用最多发信一次。
       但是pthread_cond_signal在多处理器上可能同时唤醒多个线程,当你只能让一个线程处理某个任务时,其它被唤醒的线程就需要继续 wait,而且规范要求pthread_cond_signal至少唤醒一个pthread_cond_wait上的线程,其实有些实现为了简单在单处理器上也会唤醒多个线程. 
       另外,某些应用,如线程池,pthread_cond_broadcast唤醒全部线程,但我们通常只需要一部分线程去做执行任务,所以其它的线程需要继续wait.所以强烈推荐对pthread_cond_wait() 使用while循环来做条件判断.

实例 1
pthread_cond_broadcast的使用
[cpp]  view plain  copy
  1. #include <pthread.h>  
  2. #include <stdlib.h>  
  3. #include <stdio.h>  
  4. #include <unistd.h>  
  5. #include <string.h>  
  6. pthread_mutex_t mymutex1 = PTHREAD_MUTEX_INITIALIZER;  
  7. pthread_mutex_t mymutex2 = PTHREAD_MUTEX_INITIALIZER;  
  8. pthread_cond_t mycond = PTHREAD_COND_INITIALIZER;  
  9. void *mythread1(void *param)  
  10. {  
  11.     printf("begin mythread1.\n");  
  12.     pthread_mutex_lock(&mymutex1);  
  13.     printf("wait in mythread1.\n");  
  14.     pthread_cond_wait(&mycond,&mymutex1);  
  15.     pthread_mutex_unlock(&mymutex1);  
  16.     printf("end mythread1.\n");  
  17.     return NULL;  
  18. }  
  19. void *mythread2(void *param)  
  20. {  
  21.     printf("begin mythread2.\n");  
  22.     pthread_mutex_lock(&mymutex2);  
  23.     printf("wait in mythread2.\n");  
  24.     pthread_cond_wait(&mycond,&mymutex2);  
  25.     pthread_mutex_unlock(&mymutex2);  
  26.     printf("end mythread2.\n");  
  27.     return NULL;  
  28. }  
  29. int main(void)  
  30. {  
  31.     printf("begin main thread.\n");  
  32.     int i;  
  33.     pthread_t tid1,tid2;  
  34.     pthread_create(&tid1,NULL,mythread1,NULL);  
  35.     pthread_create(&tid2,NULL,mythread2,NULL);  
  36.     sleep(2);  
  37.     printf("try to wake up mythread1 and mythread2 in main thread.\n");  
  38.     if(pthread_cond_broadcast(&mycond)){  
  39.         printf("error\n");  
  40.         return 1;  
  41.     }  
  42.     void *res;  
  43.     pthread_join(tid1, &res);  
  44.     pthread_join(tid2, &res);  
  45.     printf("end main thread.\n");  
  46.     return 0;  
  47. }  
运行结果:
begin main thread.
begin mythread1.
wait in mythread1.
begin mythread2.
wait in mythread2.
end mythread2.
try to wake up mythread1 and mythread2 in main thread.
end mythread1.
end main thread.
注意mythread2并非真正的等待,它和我们的期望有所差别,似乎一个pthread_cond_t只能对应一个pthread_mutex_t
我们把以上代码稍作修正就可以了,具体见下面的 实例3
实例3
[cpp]  view plain  copy
  1. #include <pthread.h>  
  2. #include <stdlib.h>  
  3. #include <stdio.h>  
  4. #include <unistd.h>  
  5. #include <string.h>  
  6. pthread_mutex_t mymutex1 = PTHREAD_MUTEX_INITIALIZER;  
  7. //pthread_mutex_t mymutex2 = PTHREAD_MUTEX_INITIALIZER;  
  8. pthread_cond_t mycond = PTHREAD_COND_INITIALIZER;  
  9. void *mythread1(void *param)  
  10. {  
  11.     printf("begin mythread1.\n");  
  12.     pthread_mutex_lock(&mymutex1);  
  13.     printf("wait in mythread1.\n");  
  14.     pthread_cond_wait(&mycond,&mymutex1);  
  15.     pthread_mutex_unlock(&mymutex1);  
  16.     printf("end mythread1.\n");  
  17.     return NULL;  
  18. }  
  19. void *mythread2(void *param)  
  20. {  
  21.     printf("begin mythread2.\n");  
  22.     pthread_mutex_lock(&mymutex1);  
  23.     printf("wait in mythread2.\n");  
  24.     pthread_cond_wait(&mycond,&mymutex1);  
  25.     pthread_mutex_unlock(&mymutex1);  
  26.     printf("end mythread2.\n");  
  27.     return NULL;  
  28. }  
  29. int main(void)  
  30. {  
  31.     printf("begin main thread.\n");  
  32.     int i;  
  33.     pthread_t tid1,tid2;  
  34.     pthread_create(&tid1,NULL,mythread1,NULL);  
  35.     pthread_create(&tid2,NULL,mythread2,NULL);  
  36.     sleep(2);  
  37.     printf("try to wake up mythread1 and mythread2 in main thread.\n");  
  38.     if(pthread_cond_broadcast(&mycond)){  
  39.         printf("error\n");  
  40.         return 1;  
  41.     }  
  42.     void *res;  
  43.     pthread_join(tid1, &res);  
  44.     pthread_join(tid2, &res);  
  45.     printf("end main thread.\n");  
  46.     return 0;  
  47. }  
运行结果
123@xyy ~/gcc-test
$ gcc threadTest.c -o test.exe
123@xyy ~/gcc-test
$ ./test.exe
begin main thread.
begin mythread1.
wait in mythread1.
begin mythread2.
wait in mythread2.
try to wake up mythread1 and mythread2 in main thread.
end mythread1.
end mythread1.
end main thread.
该结果才是我们真正需要的。
结束!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值