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用户线程模型 “线程实现”建立在“进程控制”机制之上,由用 ...
随机推荐
- springboot 读取 yml 配置的几种方式
前言:在springboot 项目中一般默认的配置文件是application.properties,但是实际项目中我们一般会使用application.yml 文件,下面就介绍一下在springbo ...
- 因写太多 BUG!程序员遭公司颁奖羞辱,做的一个比一个绝
刚入职的程序员新人,办公桌上,基本上也就一电脑.一键盘.一鼠标,再配个被杯子.然而混迹职场多年的猿老们,办公桌上都有一些彰显身份地位的“好东西”. 这张图两点颇多,最显眼的,是办公桌上那个黄黄的东西, ...
- 渐进式 JavaScript 框架--Vue
前 言 灵活 不断繁荣的生态系统,可以在一个库和一套完整框架之间自如伸缩. 高效 20kB min+gzip 运行大小超快虚拟 DOM 最省心的优化 1 计算属性 计算属性关键词: comp ...
- Ubuntu16.04下安装QQ的完整操作记录(经验证可用)
本机安装了Ubuntu16.04系统,用于日常运维办公.打算在Ubuntu上安装QQ,如下操作记录也使用于Ubuntu18.04: 1)先下载特制的QQ程序包(其实就是基于Wine容器做了一些封装,程 ...
- Linux下FastDFS分布式存储-总结及部署记录
一.分布式文件系统介绍分布式文件系统:Distributed file system, DFS,又叫做网络文件系统:Network File System.一种允许文件通过网络在多台主机上分享的文件系 ...
- 1017 B. The Bits
链接 [http://codeforces.com/contest/1017/problem/B] 题意 给你两个长度为n,包含0和1的字符串a和b,有一种操作swap a中的任意两个字符使得a&am ...
- Linux内核分析——第四章 进程调度
第四章 进程调度 4.1 多任务 1.多任务操作系统就是能同时并发的交互执行多个进程的操作系统. 2.多任务操作系统使多个进程处于堵塞或者睡眠状态,实际不被投入执行,这些任务尽管位于内存,但是并不处于 ...
- 《Linux内核设计与实现》第八周读书笔记——第四章 进程调度
<Linux内核设计与实现>第八周读书笔记——第四章 进程调度 第4章 进程调度35 调度程序负责决定将哪个进程投入运行,何时运行以及运行多长时间,进程调度程序可看做在可运行态进程之间分配 ...
- Orcle安装环境及步骤
Windows7环境下如何成功安装Oracle数据库 随着微软新一代操作系统 Windows7 的正式发行,使用 Windows7 的朋友也越来越多,很多人在 Windows7 环境下安装 ...
- beta(3/7)
团队信息 队名:爸爸饿了 组长博客:here 作业博客:here 组员情况 组员1(组长):王彬 过去两天完成了哪些任务 协助后端完成历史记录接口.美食排行榜接口 完成食堂平面图的绘制 确定web端业 ...