聊聊pthread_cond_wait的虚假唤醒
使用条件变量时,仅仅从pthread_cond_wait返回就说条件成立是不恰当的。我们正确使用pthread_cond_wait的唯一方式是当线程被从pthread_cond_wait唤醒时,再检查一下我们等待的条件(pthread_cond_wait的返回使得我们再次获得mutex,这时我们可以讲,我们拥有绝对的对等待条件的访问权,我们对其状态拥有绝对自信)
我看到有人把POSIX对pthread_cond_signal实现的约束(详见apue page334)作为从pthread_cond_wait返回必须进行变量值检查的原因,这不够准确,根本原因只有一个,只有当前线程获取到了被保护变量的mutex才能对被保护变量的值做推论。
为了更好的说明问题,假设有这样一个应用场景:我们使用线程A,B来消费一个值,线程C用于生产这个值,生产后就广播A与B,主线程等待他们。
#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
#include<errno.h>
#include<assert.h>
#include<signal.h> pthread_mutex_t mutex =PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER; volatile int value = ; pthread_t tid;
pthread_t tid2;
pthread_t tid3; void* thread_func(void* arg) {
pthread_mutex_lock(&mutex); if ( == value) {
pthread_cond_wait(&cond, &mutex);
}
printf("the thread_func is finished value = %d\n", value);
value = ;
pthread_mutex_unlock(&mutex);
} void* thread_func3(void* arg) {
pthread_mutex_lock(&mutex); if ( == value) {
pthread_cond_wait(&cond, &mutex);
}
printf("the thread_func3 is finished value = %d\n", value);
value = ;
pthread_mutex_unlock(&mutex);
} void* thread_func2(void* arg) {
pthread_mutex_lock(&mutex);
value = ;
pthread_cond_broadcast(&cond);
printf("the thread_func2 is finished value = %d\n", value);
pthread_mutex_unlock(&mutex);
} int
main(int argc, char* argv[])
{
// pthread_attr_t attr;
// assert(0 == pthread_attr_init(&attr));
// pthread_create(&tid2, NULL, thread_func2, NULL);
// pthread_join(tid2, NULL);
// printf("thread id = %ld\n", tid);
// printf("thread2 id = %ld\n", tid2);
// printf("thread3 id = %ld\n", tid3);
// printf("---------keep going---------\n");
// pthread_create(&tid, NULL, thread_func, NULL);
// pthread_create(&tid3, NULL, thread_func3, NULL);
// pthread_join(tid, NULL);
// pthread_join(tid3, NULL);
// pthread_cond_destroy(&cond); pthread_attr_t attr;
assert( == pthread_attr_init(&attr));
pthread_create(&tid, NULL, thread_func, NULL);
pthread_create(&tid3, NULL, thread_func3, NULL);
sleep();
printf("thread id = %ld\n", tid);
printf("thread3 id = %ld\n", tid3);
printf("make sure the thread and thread3 is created\n"); pthread_create(&tid2, NULL, thread_func2, NULL);
pthread_join(tid2, NULL);
pthread_join(tid, NULL);
pthread_join(tid3, NULL);
pthread_cond_destroy(&cond);
pthread_attr_destroy(&attr); }
程序中需要注意line70,为了使得pthread_cond_broadcast成功,这里被动等待了5s,其它的都没啥说的。
我们假设thread_func收到广播后先执行,thread_func持有mutex,所以thread_func3在从pthread_cond_wait这里返回就是失败的,虽然收到了广播,但是它压根拿不到mutex。
等到thread_fucnc释放mutex后,thread_func3开始执行,这时value的值已经不是thread_func3被通知时的值了(这个例子比较极端)。所以我们在从pthread_cond_wait返回后需要再验证我们等待的value是否是期望的值。
那我们再进一步,thread_func3从pthread_cond_wait返回后再加个if判断不就行了吗???代码如下:
...
//in the thread_func3
if( == value) {
pthread_cond_wait();
}
if( == value) {
pthread_cond_wait();
}
...
那如果又发生了thread_func抢先消费掉value的情况呢?是不是又需要判断一次value,如此往复,当然写成这样才是唯一解了:
while( == value) {
pthread_cond_wait();
}
好了,谢谢各位的阅读。恳请批评指正!
聊聊pthread_cond_wait的虚假唤醒的更多相关文章
- pthread_cond_wait虚假唤醒
pthread_cond_wait中的while()不仅仅在等待条件变量前检查条件cond_is_false是否成立,实际上在等待条件变量后也检查条件cond_is_false是否成立.在多线程等待的 ...
- 刨根问底系列(1)——虚假唤醒(spurious wakeups)的原因以及在pthread_cond_wait、pthread_cond_singal中使用while的必要性
刨根问底之虚假唤醒 1. 概要 将会以下方式展开介绍: 什么是虚假唤醒 什么原因会导致虚假唤醒(两种原因) 为什么系统内核不从根本上解决虚假唤醒这个"bug"(两个原因) 开发者如 ...
- 多线程编程中条件变量和的spurious wakeup 虚假唤醒
1. 概述 条件变量(condition variable)是利用共享的变量进行线程之间同步的一种机制.典型的场景包括生产者-消费者模型,线程池实现等. 对条件变量的使用包括两个动作: 1) 线程等待 ...
- 什么是虚假唤醒 spurious wakeup
解释一下什么是虚假唤醒? 说具体的例子,比较容易说通. pthread_mutex_t lock; pthread_cond_t notempty; pthread_cond_t notfull; v ...
- 【转】pthread_cond_signal 虚假唤醒问题
引用:http://blog.csdn.net/leeds1993/article/details/52738845 什么是虚假唤醒? 举个例子,我们现在有一个生产者-消费者队列和三个线程. I.1号 ...
- notify丢失、虚假唤醒
notify丢失: 假设线程A因为某种条件在条件队列中等待,同时线程B因为另外一种条件在同一个条件队列中等待,也就是说线程A/B都被同一个Object.wait()挂起,但是等待的条件不同. 现在假设 ...
- JUC虚假唤醒(六)
为什么条件锁会产生虚假唤醒现象(spurious wakeup)? 在不同的语言,甚至不同的操作系统上,条件锁都会产生虚假唤醒现象.所有语言的条件锁库都推荐用户把wait()放进循环里: whil ...
- (三)juc高级特性——虚假唤醒 / Condition / 按序交替 / ReadWriteLock / 线程八锁
8. 生产者消费者案例-虚假唤醒 参考下面生产者消费者案例: /* * 生产者和消费者案例 */ public class TestProductorAndConsumer { public stat ...
- Java-JUC(八):使用wait,notify|notifyAll完成生产者消费者通信,虚假唤醒(Spurious Wakeups)问题出现场景,及问题解决方案。
模拟通过线程实现消费者和订阅者模式: 首先,定义一个店员:店员包含进货.卖货方法:其次,定义一个生产者,生产者负责给店员生产产品:再者,定义一个消费者,消费者负责从店员那里消费产品. 店员: /** ...
随机推荐
- Struts2配置文件复用代码【web.xml、struts.xml、常量配置】
web.xml的分发器代码: <!-- 引入struts核心过滤器 --> <filter> <filter-name>struts2</filter-nam ...
- java基础知识1--String常用方法总结
主要涉及String常用的方法. package collection; import java.lang.reflect.Array; import java.util.Arrays; /** * ...
- String类的简要概述(1)
String类时我们平时用的比较多的一个类,该类属于java中引用数据类型. String类里面有很多方法需要我们学习.如切割,追加,拼接等. String s = "abcdef" ...
- 一步一步带你做WebApi迁移ASP.NET Core2.0
随着ASP.NET Core 2.0发布之后,原先运行在Windows IIS中的ASP.NET WebApi站点,就可以跨平台运行在Linux中.我们有必要先说一下ASP.NET Core. ASP ...
- 新建maven项目遇到Select an Archetype时没有maven-archetype-webapp处理方法
[已经有很多博客写过相关的了.详细请去看其他博主的.这里只是记录新建的时候发生的问题给新手提供帮助.因为我跟我的同事都遇到了.因为没记录下来,又花了时间找问题.而网上好像也不多.所以记录下来.希望帮到 ...
- 【专章】dp基础
知识储备:dp入门. 好了,完成了dp入门,我们可以做一些稍微不是那么裸的题了. ----------------------------------------------------------- ...
- Painter's Problem poj1681 高斯消元法
Painter's Problem Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4420 Accepted: 2143 ...
- An Introduction to Variational Methods (5.2)
我们现在已经得到了关于潜在变量Z的优化分布的表达形式: 其中: 所以现在我们可以得到Z的期望: 另外对于Z还值得一提的是,我们从其优化分布的表达式中可以看出,各个Z的组成部分之间还是相互耦 ...
- 解决子级用css float浮动 而父级div没高度不能自适应高度
解决子级对象使用css float浮动 而父级div不能自适应高度,不能被父级内容撑开解决方法,父级div没有高度解决方法. 最外层的父级DIV不能自适应高度-不能随对象撑开没有高度 当在对象内的盒子 ...
- springMVC中的redirect和forward区别?
1.forward在跳转后可以取到message值,redirect在跳转后无法取到message值. 2.forward跳转后地址栏URL不会改变,而redirect会改变.