转:http://blog.chinaunix.net/uid-11572501-id-3456343.html

//pthread_cond_signal 只发信号,内部不会解锁,在Linux 线程中,有两个队列,分别是cond_wait队列和mutex_lock队列, cond_signal只是让线程从cond_wait队列移到mutex_lock队列,而不用返回到用户空间,不会有性能的损耗。(pthread_cond_signal unlock后pthread_cond_wait才能上锁)

//pthread_cond_wait 先解锁,等待,有信号来,上锁,执行while检查防止另外的线程更改条件

//循环判断的原因如下:假设2个线程在getq阻塞,然后两者都被激活,而其中一个线程运行比较块,快速消耗了2个数据,另一个线程醒来的时候已经没有新 数据可以消耗了。另一点,man pthread_cond_wait可以看到,该函数可以被信号中断返回,此时返回EINTR。为避免以上任何一点,都必须醒来后再次判断睡眠条件。更 正:pthread_cond_wait是信号安全的系统调用,不会被信号中断, (不是说wai来到后再走一次while)

条件锁(条件Mutex)pthread_cond_wait、pthread_cond_signal、pthread_cond_broadcast的使用收藏 

    pthread_cond_wait()的工作流程如下(以MAN中的EXAMPLE为例): 
       Consider two shared variables x and y, protected by the mutex mut, and a condition vari- 
       able cond that is to be signaled whenever x becomes greater than y. 

              int x,y; 
              pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER; 
              pthread_cond_t cond = PTHREAD_COND_INITIALIZER; 

       Waiting until x is greater than y is performed as follows: 

              pthread_mutex_lock(&mut); 
              while (x <= y) { 
                      pthread_cond_wait(&cond, &mut); 
              } 
              /* operate on x and y */ 
              pthread_mutex_unlock(&mut); 

       Modifications on x and y that may cause x to become greater than y should signal the con- 
 dition if needed: 

              pthread_mutex_lock(&mut); 
              /* modify x and y */ 
              if (x > y) pthread_cond_singal(&cond); 
              pthread_mutex_unlock(&mut); 

 
   这个例子的意思是,两个线程要修改X和
Y的值,第一个线程当X<=Y时就挂起,直到X>Y时才继续执行(由第二个线程可能会修改X,Y的值,当X>Y时唤醒第一个线程),即
首先初始化一个普通互斥量mut和一个条件变量cond。之后分别在两个线程中分别执行如下函数体: 

              pthread_mutex_lock(&mut); 
              while (x <= y) { 
                      pthread_cond_wait(&cond, &mut); 
              } 
              /* operate on x and y */ 
              pthread_mutex_unlock(&mut); 
和:       pthread_mutex_lock(&mut); 
              /* modify x and y */ 
              if (x > y) pthread_cond_signal(&cond); 
              pthread_mutex_unlock(&mut); 
 
 
其实函数的执行过程非常简单,在第一个线程执行到pthread_cond_wait(&cond,&mut)时,此时如果
X<=Y,则此函数就将mut互斥量解锁,再将cond条件变量加锁 ,此时第一个线程挂起 (不占用任何CPU周期)。 
 
 
而在第二个线程中,本来因为mut被第一个线程锁住而阻塞,此时因为mut已经释放,所以可以获得锁mut,并且进行修改X和Y的值,在修改之后,一个
IF语句判定是不是X>Y,如果是,则此时pthread_cond_signal()函数会唤醒第一个线程,并在下一句中释放互斥量mut。然后
第一个线程开始从pthread_cond_wait()执行,首先要再次锁mut , 如果锁成功,再进行条件的判断
(至于为什么用WHILE,即在被唤醒之后还要再判断,后面有原因分析),如果满足条件,则被唤醒进行处理,最后释放互斥量mut 。 

 
 
至于为什么在被唤醒之后还要再次进行条件判断(即为什么要使用while循环来判断条件),是因为可能有“惊群效应”。有人觉得此处既然是被唤醒的,肯定
是满足条件了,其实不然。如果是多个线程都在等待这个条件,而同时只能有一个线程进行处理,此时就必须要再次条件判断,以使只有一个线程进入临界区处理。
对此,转来一段: 

引用下POSIX的RATIONALE: 

Condition Wait Semantics 

It
is important to note that when pthread_cond_wait() and
pthread_cond_timedwait() return without error, the associated predicate
may still be false. Similarly, when pthread_cond_timedwait() returns
with the timeout error, the associated predicate may be true due to an
unavoidable race between the expiration of the timeout and the predicate
state change.

The
application needs to recheck the predicate on any return because it
cannot be sure there is another thread waiting on the thread to handle
the signal, and if there is not then the signal is lost. The burden is
on the application to check the predicate. 

Some
implementations, particularly on a multi-processor, may sometimes cause
multiple threads to wake up when the condition variable is signaled
simultaneously on different processors. 

In
general, whenever a condition wait returns, the thread has to
re-evaluate the predicate associated with the condition wait to
determine whether it can safely proceed, should wait again, or should
declare a timeout. A return from the wait does not imply that the
associated predicate is either true or false. 

It is thus recommended that a condition wait be enclosed in the equivalent of a "while loop" that checks the predicate. 

从上文可以看出: 
1,pthread_cond_signal
在多处理器上可能同时唤醒多个线程,当你只能让一个线程处理某个任务时,其它被唤醒的线程就需要继续
wait,while循环的意义就体现在这里了,而且规范要求pthread_cond_signal至少唤醒一个pthread_cond_wait上
的线程,其实有些实现为了简单在单处理器上也会唤醒多个线程. 
2,某些应用,如线程池,pthread_cond_broadcast唤醒全部线程,但我们通常只需要一部分线程去做执行任务,所以其它的线程需要继续wait.所以强烈推荐此处使用while循环. 

       其实说白了很简单,就是pthread_cond_signal()也可能唤醒多个线程,而如果你同时只允许一个线程访问的话,就必须要使用while来进行条件判断,以保证临界区内只有一个线程在处理。

pthread_cond_signal与pthread_cond_wait详解的更多相关文章

  1. pthread_cond_wait 详解

    转自:http://www.xuebuyuan.com/2173853.html pthread_cond_wait() 用于阻塞当前线程,等待别的线程使用pthread_cond_signal()或 ...

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

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

  3. 转载~kxcfzyk:Linux C语言多线程库Pthread中条件变量的的正确用法逐步详解

    Linux C语言多线程库Pthread中条件变量的的正确用法逐步详解   多线程c语言linuxsemaphore条件变量 (本文的读者定位是了解Pthread常用多线程API和Pthread互斥锁 ...

  4. 通用线程:POSIX 线程详解,第 3 部分 条件互斥量(pthread_cond_t)

    使用条件变量提高效率 本文是 POSIX 线程三部曲系列的最后一部分,Daniel 将详细讨论如何使用条件变量.条件变量是 POSIX 线程结构,可以让您在遇到某些条件时“唤醒”线程.可以将它们看作是 ...

  5. 通用线程:POSIX 线程详解,第 3 部分

    通用线程:POSIX 线程详解,第 3 部分 使用条件变量提高效率 Daniel Robbins, 总裁兼 CEO, Gentoo Technologies, Inc. 简介: 本文是 POSIX 线 ...

  6. POSIX 线程详解(经典必看)

    http://www.cnblogs.com/sunminmin/p/4479952.html 总共三部分: 第一部分:POSIX 线程详解                               ...

  7. JUC中的AQS底层详细超详解

    摘要:当你使用java实现一个线程同步的对象时,一定会包含一个问题:你该如何保证多个线程访问该对象时,正确地进行阻塞等待,正确地被唤醒? 本文分享自华为云社区<JUC中的AQS底层详细超详解,剖 ...

  8. Linq之旅:Linq入门详解(Linq to Objects)

    示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细介绍 .NET 3.5 中引入的重要功能:Language Integrated Query(LINQ,语言集 ...

  9. 架构设计:远程调用服务架构设计及zookeeper技术详解(下篇)

    一.下篇开头的废话 终于开写下篇了,这也是我写远程调用框架的第三篇文章,前两篇都被博客园作为[编辑推荐]的文章,很兴奋哦,嘿嘿~~~~,本人是个很臭美的人,一定得要截图为证: 今天是2014年的第一天 ...

随机推荐

  1. [bzoj1711]吃饭

    由于无法直接将果汁和饮料连边,所以将人放在中间,果汁和饮料放在两侧,然后分别向对应的人连边.同时,为了保证每一个人只被算一次,对每一个人裂点,两个点中间连一条流量为1的边. 1 #include< ...

  2. [bzoj5462]新家

    先离线,将询问按照时间排序,维护商店出现和消失 对于每一个询问,先二分枚举答案,假设是ans, 即要求对于区间[l-ans,l+ans],商店的种类数是k(l是询问的位置) (当然需要先将所有位置离散 ...

  3. Winform开发的快速、健壮、解耦的几点建议

    在Winform开发领域开发过十多年的项目中,见证着形形色色的架构和官方技术的应用,从最早类似Winform模式的WebForm技术,到接着的JQuery+界面组件,再到Asp.net Core的技术 ...

  4. [JS高程] 特殊的原始值类型

    目录 原始值包装类型 (特殊引用类型) 原始值包装类型和应用类型的区别 原始值包装类型 (特殊引用类型) 在ES6 时,ECMAScript 数据类型是这样去分类的: 原始值(基本数据类型) Numb ...

  5. dart系列之:在dart中使用数字和字符串

    目录 简介 数字 字符串 StringBuffer 总结 简介 要想熟悉一种语言,最简单的做法就是熟悉dart提供的各种核心库.dart为我们提供了包括dart:core,dart:async,dar ...

  6. Hibernate数据校验简介

    我们在业务中经常会遇到参数校验问题,比如前端参数校验.Kafka消息参数校验等,如果业务逻辑比较复杂,各种实体比较多的时候,我们通过代码对这些数据一一校验,会出现大量的重复代码以及和主要业务无关的逻辑 ...

  7. 深入理解Redis 数据结构—双链表

    在 Redis 数据类型中的列表list,对数据的添加和删除常用的命令有 lpush,rpush,lpop,rpop,其中 l 表示在左侧,r 表示在右侧,可以在左右两侧做添加和删除操作,说明这是一个 ...

  8. P4550 收集邮票 与 灵异的期望

    考前复习一下期望相关知识,这题的期望还是很巧妙的. 设 \(f_{i}\) 表示已经买到了 \(i\) 张不同的邮票的期望步数,\(g_{i}\) 表示表示已经买到了 \(i\) 张不同的邮票的期望花 ...

  9. Secant 方法求方程多个根

    Secant 方法介绍 Secant Method 函数 Secant_Methods 简介 1.函数定义 [c, errColumn] = Secant_Method(f, a, b, N, con ...

  10. mysql-计算排名

    mysql计算排名,获取行号rowno 学生成绩表数据 SELECT * FROM table_score ORDER BY score DESC; 获取某个学生成绩排名并计算该学生和上一名学生成绩差 ...