pthread_cond_broadcast(&cond1)的作用是唤醒所有正在pthread_cond_wait(&cond1,&mutex1)的线程。

pthread_cond_signal(&cond1)的的作用是唤醒所有正在pthread_cond_wait(&cond1,&mutex1)的至少一个线程。(虽然我还没碰到过多于一个线程的情况,但是man帮组手册上说的是至少一个)

下面分为情况讨论一下这两个函数的效果。

第一种情况:多个线程等待同一个cond,并且想对同一个mutex加锁。

 1 #include <stdio.h>2 #include <unistd.h>3 #include <pthread.h>4 #include <stdlib.h>5 6 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;7 pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;8 pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;9
10 void* thread_task1(void* arg)
11 {
12     pthread_mutex_lock(&mutex1);
13
14     pthread_cond_wait(&cond,&mutex1);
15
16     printf("thread_task1 start working\n");
17     sleep(2);
18     printf("thread_task1 works over\n");
19     pthread_mutex_unlock(&mutex1);
20
21     return NULL;
22
23
24
25 }
26
27 void* thread_task2(void* arg)
28 {
29     pthread_mutex_lock(&mutex1);
30
31     pthread_cond_wait(&cond,&mutex1);
32
33     printf("thread_task2 start working\n");
34     sleep(2);
35     printf("thread_task2 works over\n");
36     pthread_mutex_unlock(&mutex1);
37
38     return NULL;
39
40
41 }
42
43 void* broadcastDiffMutex(void* arg)
44 {
45     pthread_cond_broadcast(&cond);
46     return NULL;
47
48 }
49
50 void* signalDiffMutex(void* arg)
51 {
52     pthread_cond_signal(&cond);
53     return NULL;
54
55 }
56
57 int main()
58 {
59     pthread_t thread_1,thread_2,thread_3;
60     pthread_create(&thread_1,NULL,thread_task1,NULL);
61     pthread_create(&thread_2,NULL,thread_task2,NULL);
62     sleep(2);
63
64 #ifdef SIGNAL
65     pthread_create(&thread_3,NULL,signalDiffMutex,NULL);
66 #else
67     pthread_create(&thread_3,NULL,broadcastDiffMutex,NULL);
68 #endif
69
70
71     pthread_join(thread_1,NULL);
72     pthread_join(thread_2,NULL);
73     pthread_join(thread_3,NULL);
74
75     return 0;
76
77 }

使用broadcast的运行结果:

使用signal的运行结果:

分析:

  1. 当使用broadcast方式时,两个被阻塞的线程都被唤醒了,被唤醒的线程将变为pthread_mutex_lock(mutex1)的状态,他们将抢着对mutex1加锁,在本次运行过程中thread_1加锁成功了,thread_2没有成功抢到锁,于是它就被阻塞了,在thread_1执行完毕释放锁后,会通知所有被阻塞在mutex1上的线程,于是thread_2最终成功拿到了锁,然后顺利执行。
  2. 当使用signal方式时,thread_1和thread_2中只被唤醒了一个线程,在本次运行中是thread_1被唤醒了,而因为thread_2没有被唤醒,他就一直卡在pthread_cond_wait处呼呼大睡,所以最终只有thread_1执行完毕。

第二种情况:多个线程等待同一个cond,并且分别不同的mutex加锁。

 1 #include <stdio.h>2 #include <unistd.h>3 #include <pthread.h>4 #include <stdlib.h>5 6 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;7 pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;8 pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;9
10 void* thread_task1(void* arg)
11 {
12     pthread_mutex_lock(&mutex1);
13
14     pthread_cond_wait(&cond,&mutex1);
15
16     printf("thread_task1 start working\n");
17     sleep(2);
18     printf("thread_task1 works over\n");
19     pthread_mutex_unlock(&mutex1);
20
21     return NULL;
22
23
24
25 }
26
27 void* thread_task2(void* arg)
28 {
29     pthread_mutex_lock(&mutex2);
30
31     pthread_cond_wait(&cond,&mutex2);
32
33     printf("thread_task2 start working\n");
34     sleep(2);
35     printf("thread_task2 works over\n");
36     pthread_mutex_unlock(&mutex2);
37
38     return NULL;
39
40
41 }
42
43 void* broadcastDiffMutex(void* arg)
44 {
45     pthread_cond_broadcast(&cond);
46     return NULL;
47
48 }
49
50 void* signalDiffMutex(void* arg)
51 {
52     pthread_cond_signal(&cond);
53     return NULL;
54
55 }
56
57 int main()
58 {
59     pthread_t thread_1,thread_2,thread_3;
60     pthread_create(&thread_1,NULL,thread_task1,NULL);
61     pthread_create(&thread_2,NULL,thread_task2,NULL);
62     sleep(2);
63
64 #ifdef SIGNAL
65     pthread_create(&thread_3,NULL,signalDiffMutex,NULL);
66 #else
67     pthread_create(&thread_3,NULL,broadcastDiffMutex,NULL);
68 #endif
69
70
71     pthread_join(thread_1,NULL);
72     pthread_join(thread_2,NULL);
73     pthread_join(thread_3,NULL);
74
75     return 0;
76
77 }

使用broadcast的效果:

使用signal的效果

 分析:

  1. 当使用broadcast方式时,因为两个线程都被唤醒了,且它们想要加的锁并没有竞争关系,因此它们是并发执行的,而不必像前一种情况中那样必须一前一后执行。
  2. 当使用signal方式时,只被唤醒了一个线程,因此只有一个线程成功执行。

pthread_cond_broadcast pthread_cond_signal相关推荐

  1. PulseAudio多线程通信:pthread_cond_broadcast/pthread_cond_signal/pthread_cond_wait(九)

    pthread_cond_broadcast() pthread_cond_signal() pthread_cond_wait()函数用法概述1.pthread_cond_wait() 用于阻塞当前 ...

  2. linux C语言多线程库pthread中条件变量的正确用法逐步详解

    linux C语言多线程库pthread中条件变量的正确用法: 了解pthread常用多线程API和pthread互斥锁,但是对条件变量完全不知道或者不完全了解的人群. 关于条件变量的典型应用,可以参 ...

  3. QNX Neutrino 微内核

    QNX Neutrino 微内核   微内核实现了POSIX特性,连同基本的QNX中微子消息传递服务,用于嵌入式realtime 操作系统.   在procnto微内核中(file and devic ...

  4. QNX system architecture -- Chapter 2:The QNX Neutrino Microkernel

    microkernel实现了嵌入式实时系统中使用的核心POSIX功能,以及基本的QNX Neutrino消息传递服务. 有些POSIX功能(如file, device I/O)不是在procnto m ...

  5. QNX system architecture 2 - the QNX Neutrino Microkernel

    microkernel实现了嵌入式实时系统使用的POSIX核心功能,以及QNX的消息传递服务. 有些POSIX功能(如file, device I/O)不是在procnto microkernel中实 ...

  6. pthread_cond_signal与pthread_cond_broadcast的使用区别

    简言之: pthread_cond_t cond; pthread_cond_broadcast(&cond)可以唤醒多个跟条件变量cond绑定的锁 一个生产者多消费者,生产者能一次产生多个产 ...

  7. 关于pthread_cond_signal与pthread_cond_broadcast的使用说明

    在code review中,我会发现很多人喜欢在pthread_mutex_lock()和pthread_mutex_unlock(()之间调用pthread_cond_signal或者pthread ...

  8. 条件变量pthread_cond_wait()和pthread_cond_signal()详解

    条件变量          条件变量是利用线程间共享的全局变量进行同步的一种机制,主要包括两个动作:一个线程等待"条件变量的条件成立"而挂起:另一个线程使"条件成立&qu ...

  9. pthread_cond_broadcast相关

    参考:http://blog.csdn.net/cupidove/article/details/9331859 pthread_cond_timedwait()函数阻塞住调用该函数的线程,等待由co ...

最新文章

  1. 分析PHP中单双引号的误区和双引号小隐患
  2. 深度学习算法实践(基于Theano和TensorFlow)
  3. empty vocabulary; perhaps the documents only contain stop words
  4. mysql 添加多条数据类型_向数据库添加多条数据类型
  5. jupyter修改密码后无法启动服务器,搭建jupyter远程连接服务器
  6. 开发转测试没人要_前端开发,测试,后端,该如何选择?
  7. C语言判断系统是32位还是64位
  8. 为什么计算机关机慢,电脑关机慢是什么原因 电脑关机慢的原因【图文】
  9. ssh不能连接 提示WARNING: POSSIBLE DNS SPOOFING DETECTED!处理方法
  10. Java集合——概述
  11. 有关于类的定义赋值与调用总结
  12. 汉字书写解码_汉字密码 | 500个字根即可解码10万个汉字,《说文解字》的神功...
  13. 单片机原理与接口技术期末总复习
  14. python创建单行文本框_HTML单行文本框
  15. Java总结一:初窥线程
  16. 力扣刷题 DAY_74 回溯
  17. requests中get请求没有referer得不到数据
  18. 干货|人脸识别技术基础知识,看这里
  19. 红帽认证工程师(RHCE)的发展前景
  20. http 请求的7 种方法

热门文章

  1. GBase 8s 入门
  2. android app文件夹,android app文件目录结构
  3. 大促在即,拥有亿级流量的电商平台开发了一个订单系统,我们应该如何来预估其并发量?如何根据并发量来合理配置JVM参数呢?
  4. 新手小白怎么学抖音运营?抖音运营5大技巧
  5. php 去除多余空行,php如何去除空行
  6. PPT VBA:批量转PDF
  7. 【2023】浙江大学计算机考研信息汇总
  8. android 9.x MTK平台讯飞输入法重启被卸载
  9. 1138:破解简单密码
  10. js 实现简单区块链