pthread_cond_wait() 函数的使用
1. 首先pthread_cond_wait 的定义是这样的
The pthread_cond_wait() and pthread_cond_timedwait() functions are used to block on a condition variable. They are called with mutex locked by the calling thread or undefined behaviour will result.
These functions atomically release mutex and cause the calling thread to block on the condition variablecond; atomically here means "atomically with respect to access by another thread to the mutex and then the condition variable". That is, if another thread is able to acquire the mutex after the about-to-block thread has released it, then a subsequent call to pthread_cond_signal() or pthread_cond_broadcast() in that thread behaves as if it were issued after the about-to-block thread has blocked.
2. 由上解释可以看出,pthread_cond_wait() 必须与pthread_mutex 配套使用。
pthread_cond_wait()函数一进入wait状态就会自动release mutex.
In Thread1:
pthread_mutex_lock(&m_mutex);
pthread_cond_wait(&m_cond,&m_mutex);
pthread_mutex_unlock(&m_mutex);
In Thread2:
pthread_mutex_lock(&m_mutex);
pthread_cond_signal(&m_cond);
pthread_mutex_unlock(&m_mutex);
为什么要与pthread_mutex 一起使用呢? 这是为了应对 线程1在调用pthread_cond_wait()但线程1还没有进入wait cond的状态的时候,此时线程2调用了 cond_singal 的情况。 如果不用mutex锁的话,这个cond_singal就丢失了。加了锁的情况是,线程2必须等到 mutex 被释放(也就是 pthread_cod_wait() 进入wait_cond状态 并自动释放mutex) 的时候才能调用cond_singal.
3.
pthread_cond_wait() 一旦wait成功获得cond 条件的时候会自动 lock mutex.
这就会出现另一个问题。这是因为
The pthread_cond_wait() and pthread_cond_timedwait() is a cancellation point.
In Thread3:
pthread_cancel(&m_thread);
pthread_join();
因为pthread_cond_wait() and pthread_cond_timedwait() 是线程退出点函数,因此在Thread3中
可以调用pthread_cancel()来退出线程1。那样显然线程1会在pthread_cond_wait(&m_cond,&m_mutex); 和 pthread_mutex_unlock(&m_mutex); 之间退出, pthread_cond_wait() 函数返回后自动lock住了mutex, 这个时候线程1退出(并没有运行到pthread_mutex_unlock()),如果Thread2这个时候就再也得不到lock状态了。
通常解决这个问题的办法如下
void cleanup(void *arg)
{
pthread_mutex_unlock(&mutex);
}
void* thread1(void* arg)
{
pthread_cleanup_push(cleanup, NULL); // thread cleanup handler
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond, &mutex);
pthread_mutex_unlock(&mutex);
pthread_cleanup_pop(0);
}
pthread_cond_wait() 函数的使用的更多相关文章
- 互斥量、条件变量与pthread_cond_wait()函数的使用,详解(一)
1. 首先pthread_cond_wait 的定义是这样的 The pthread_cond_wait() and pthread_cond_timedwait() functions are us ...
- pthread_cond_wait函数的学习以及其他
pthread_cond_wait() 前使用 while 讲解2009-10-27 9:33LINUX环境下多线程编程肯定会遇到需要条件变量的情况,此时必然要使用pthread_cond_wait( ...
- pthread_cond_wait()函数的理解(摘录)
/************pthread_cond_wait()的使用方法**********/pthread_mutex_lock(&qlock); /*lock*/pthread_c ...
- 互斥量、条件变量与pthread_cond_wait()函数的使用,详解(二)
1.Linux“线程” 进程与线程之间是有区别的,不过linux内核只提供了轻量进程的支持,未实现线程模型.Linux是一种“多进程单线程”的操作系统.Linux本身只有进程的概念,而其所谓的“线程” ...
- pthread_cond_wait()函数的详解
http://hi.baidu.com/tjuer/item/253cc6d66b921317d90e4483 了解 pthread_cond_wait() 的作用非常重要 -- 它是 POSIX 线 ...
- pthread_cond_wait避免线程空转
多线程对同一块区域进行操作时,需要定义如下两种类型的变量: pthread_mutex_t xxx; pthread_cond_t yyy; pthread_mutex_t类型的变量,即锁,对公共区域 ...
- 深入理解pthread_cond_wait、pthread_cond_signal
===============================man pthread_cond_wait的解释========================== LINUX环境下多线程编程肯定会遇到 ...
- 线程同步,条件变量pthread_cond_wait
与互斥锁不同,条件变量是用来等待而不是用来上锁的.条件变量用来自动阻塞一个线程,直到某特殊情况发生为止.条件变量使我们可以睡眠等待某种条件出现.条件变量是利用线程间共享的全局变量进行同步的一种机制,主 ...
- 线程模型、pthread 系列函数 和 简单多线程服务器端程序
一.线程有3种模型,分别是N:1用户线程模型,1:1核心线程模型和N:M混合线程模型,posix thread属于1:1模型. (一).N:1用户线程模型 “线程实现”建立在“进程控制”机制之上,由用 ...
随机推荐
- R实战 第十一篇:处理缺失值
在真实的世界中,缺失数据是经常出现的,并可能对分析的结果造成影响.在R中,经常使用VIM(Visualization and Imputation of Missing values)包来对缺失值进行 ...
- layui表格和弹出框的简单示例
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="C ...
- 解密自动CPS变换
7.2 1 前言 我最一开始听到 CPS 变换这个词是在王垠的博客里 (请求不要喷我),就是那篇他第一次宣传他的40行代码的文章. 我当时什么都看不懂,所以没太注意,不过我也正在学程序语言方面的东西, ...
- <<架构漫谈>>读后感
今天按照老师的要求,看了架构漫谈1--9讲,觉得受益良多,以下是我得点点读后感: (一)什么是架构? 架构的英文是Architecture,从定义上看,架构好像是一个过程,也不是很清晰.下面从架构的缘 ...
- 初学Java必写的小程序。
1.矩形面积,周长封装测试. /** * @author Administrator *封装好的矩形类 *自己私有的长宽属性 *开放 求面积求周长的方法 和设置长宽的方法 */ public clas ...
- 软件工程项目之摄影App(总结)
软件工程项目之摄影App 心得体会: dyh:这次的项目很难做,本来想在里面添加动画效果的,但是找了很多例子都没看明白,能力还是不足够把,还有一个就是数据库在安卓课程里面刚刚涉及到,所以也还没能做出数 ...
- HDOJ2004_成绩转换
水题:用数组标识各个阶段分数的等级即可. HDOJ2004_成绩转换 #include<stdio.h> #include<stdlib.h> #include<math ...
- shell脚本--输入与输出
输出带有转义字符的内容 单独一个echo表示一个换行 使用echo输出时,每一条命令之后,都默认加一个换行:要想取消默认的换行,需要加 -n 参数. #!/bin/bash #文件名:test.sh ...
- window.setTimeout
https://developer.mozilla.org/zh-CN/docs/Web/API/Window/setTimeout
- MyBatis 集合操作语法范例:配合SQL的in关键字
Java语法: private String[] tagIds; MyBatis语法 <delete id="deleteByIds" parameterType=" ...