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

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

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

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

 #include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h> pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER; void* thread_task1(void* arg)
{
pthread_mutex_lock(&mutex1); pthread_cond_wait(&cond,&mutex1); printf("thread_task1 start working\n");
sleep();
printf("thread_task1 works over\n");
pthread_mutex_unlock(&mutex1); return NULL; } void* thread_task2(void* arg)
{
pthread_mutex_lock(&mutex2); pthread_cond_wait(&cond,&mutex2); printf("thread_task2 start working\n");
sleep();
printf("thread_task2 works over\n");
pthread_mutex_unlock(&mutex2); return NULL; } void* broadcastDiffMutex(void* arg)
{
pthread_cond_broadcast(&cond);
return NULL; } void* signalDiffMutex(void* arg)
{
pthread_cond_signal(&cond);
return NULL; } int main()
{
pthread_t thread_1,thread_2,thread_3;
pthread_create(&thread_1,NULL,thread_task1,NULL);
pthread_create(&thread_2,NULL,thread_task2,NULL);
sleep(); #ifdef SIGNAL
pthread_create(&thread_3,NULL,signalDiffMutex,NULL);
#else
pthread_create(&thread_3,NULL,broadcastDiffMutex,NULL);
#endif pthread_join(thread_1,NULL);
pthread_join(thread_2,NULL);
pthread_join(thread_3,NULL); return ; }

使用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加锁。

 #include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h> pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER; void* thread_task1(void* arg)
{
pthread_mutex_lock(&mutex1); pthread_cond_wait(&cond,&mutex1); printf("thread_task1 start working\n");
sleep();
printf("thread_task1 works over\n");
pthread_mutex_unlock(&mutex1); return NULL; } void* thread_task2(void* arg)
{
pthread_mutex_lock(&mutex2); pthread_cond_wait(&cond,&mutex2); printf("thread_task2 start working\n");
sleep();
printf("thread_task2 works over\n");
pthread_mutex_unlock(&mutex2); return NULL; } void* broadcastDiffMutex(void* arg)
{
pthread_cond_broadcast(&cond);
return NULL; } void* signalDiffMutex(void* arg)
{
pthread_cond_signal(&cond);
return NULL; } int main()
{
pthread_t thread_1,thread_2,thread_3;
pthread_create(&thread_1,NULL,thread_task1,NULL);
pthread_create(&thread_2,NULL,thread_task2,NULL);
sleep(); #ifdef SIGNAL
pthread_create(&thread_3,NULL,signalDiffMutex,NULL);
#else
pthread_create(&thread_3,NULL,broadcastDiffMutex,NULL);
#endif pthread_join(thread_1,NULL);
pthread_join(thread_2,NULL);
pthread_join(thread_3,NULL); return ; }

使用broadcast的效果:

使用signal的效果

 分析:

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

  

pthread_cond_broadcast & pthread_cond_signal的更多相关文章

  1. (九) 一起学 Unix 环境高级编程 (APUE) 之 线程

    . . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...

  2. Unix网络编程第三版源码编译

    配置: $ cd Unix-Network-Programming/ $ chmod 755 configure $ ./configure 主要的工作是检查系统是否有源码编译所依赖的各种资源(系统版 ...

  3. Why does pthread_cond_signal not work?【转】

    转自:http://stackoverflow.com/questions/16819169/why-does-pthread-cond-signal-not-work# 0 down vote fa ...

  4. 深入理解pthread_cond_wait、pthread_cond_signal

    ===============================man pthread_cond_wait的解释========================== LINUX环境下多线程编程肯定会遇到 ...

  5. 线程相关函数(6)-pthread_cond_wait(),pthread_cond_signal(), 条件变量

    pthread_cond_tpthread_cond_initpthread_cond_destroypthread_cond_waitpthread_cond_timedwaitpthread_co ...

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

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

  7. pthread_cond_signal与pthread_cond_wait详解

    转:http://blog.chinaunix.net/uid-11572501-id-3456343.html //pthread_cond_signal 只发信号,内部不会解锁,在Linux 线程 ...

  8. pthread_cond_signal惊群现象

    1.如下代码所示: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include < ...

  9. 关于 pthread_cond_wait 和 pthread_cond_signal , signal 无效的问题

    关于一个消费者模式,,,引起的问题.. 我在io线程里不断的把一个函数调用放到队列里 然后ruby线程就不断的从这个队列里取出函数之争并运行. 典型的 消费者模式. 我曾经以为是这样... 这是wor ...

随机推荐

  1. Redhat6 RPM 软件管理常用命令汇总

    软件的安装时操作系统管理的基础,与Windows不同,Linux的软件管理有很多种方式,Redhat的最常用的是RPM方式,安装集成在光盘中的RPM包.这种方式比Windows平台的软件管理更加便捷( ...

  2. C# 使用nuget.exe发布类库及更新类库

    前景:在开发学习阶段希望一些重复使用代码或者算法代码积累.能够在VS中下载安装方便使用. 准备工作: 1.Nuget登录账号(可 Microsoft 账号).Nuget官网 2.Nuget.exe程序 ...

  3. 在浏览器窗口内移动的div

    ------------今天研究了一个最简单的屏保效果----------- 效果图如下:效果很神奇,就是这个div在浏览器窗口不断的灵活移动 代码却很简单 <!DOCTYPE html> ...

  4. ES6的Object.assign()基本用法

    Object.assign方法用于对象的合并,将源对象(source)的所有可枚举属性,复制到目标对象(target). 例如: const target = {a:1}, const source1 ...

  5. Java 几道常见String面试题

    String s1="abc"; String s2="abc"; System.out.println(s1==s2); System.out.println ...

  6. Webpack实战(一):Webpack打包工具安装及参数配置

    为什么要模块化 javascript跟其他开发语言有很多的区别,其中一个就是没有模块化概念,如果一个项目中有多个js文件,我们只能通过script标签引入的方式,把一个个js文件插入到页面,这种做法会 ...

  7. python 学习爬虫教程~

    思路:: (本文没有用xpath定位,xpath需要导入第三方库   from lxml import etree) 1.首先通过urllib类获取到网页的所有内容 2.通过partition获取其中 ...

  8. Java电商支付系统手把手实现(二) - 数据库表设计的最佳实践

    1 数据库设计 1.1 表关系梳理 仔细思考业务关系,得到如下表关系图 1.2 用户表结构 1.3 分类表结构 id=0为根节点,分类其实是树状结构 1.4 商品表结构 注意价格字段的类型为 deci ...

  9. QT 获取当前毫秒级时间缀

    long long currentTime = QDateTime::currentDateTime().toMSecsSinceEpoch();

  10. 《大道至简》第一章Java伪代码读后感

    /*写程序,实际是一种方法论.从另外一个角度帮我们看待世界,看清事物的本质. 早在两千年前的寓言中,愚公和智叟的问答中就已体现整个工程的实现程序.*/ public class 移山{ string ...