pthread_cond_wait学习笔记
pthread_cond_wait学习笔记
近期学习了线程等待和激活的相关知识。
先介绍几个api:
pthread_cond_t表示多线程的条件变量,用于控制线程等待和就绪的条件。
一:条件变量的初始化:
条件变量和互斥锁一样,都有静态动态两种创建方式,
静态方式使用PTHREAD_COND_INITIALIZER常量初始化。
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
动态方式初始化:
1 首先要new或者malloc一个pthread_cond_t类型变量,
用完后记得delete或者free掉。
2
使用pthread_cond_wait方式如下:
pthread _mutex_lock(&mutex)
while或if(线程执行的条件是否成立)
pthread_cond_wait(&cond, &mutex);
线程执行
pthread_mutex_unlock(&mutex);
对于这点apue给出的解释:The mutex passed to pthread_cond_wait protects the condition.The caller passes it locked to
the function, which then atomically places the calling thread on the list of threads waiting for the condition and unlocks
the mutex. This closes the window between the time that the condition is checked and the time that the
thread goes to sleep waiting for the condition to change, so that the thread doesn't miss a change in the condition.
When pthread_cond_wait returns, the mutex is again locked.
1,线程放在等待队列上,解锁
2,等待 pthread_cond_signal或者pthread_cond_broadcast信号之后去竞争锁
3,若竞争到互斥索则加锁。
使用流程
等待线程:
pthread_mutex_lock(&mutex);
if(条件不满足)
pthread_cond_wait(&cond, &mutex);
//处理共享资源
pthread_mutex_unlock(&mutex);
激活线程:
pthread_mutex_lock(&mutex);
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
下面写了一个例子

#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <iostream>
using namespace std;
int count = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
//该函数增加count数值
void * creator(void * arg)
{
cout << "creator add lock" << endl;
pthread_mutex_lock(&mutex);
count ++;
cout << "in creator count is : " << count << endl;
//条件满足时发送信号
if(count > 0)
{
pthread_cond_signal(&cond);
}
cout << "creator release lock" << endl;
pthread_mutex_unlock(&mutex);
return NULL;
}
//该函数减少count数值
void * consumer(void * arg)
{
cout << "consumer add lock" << endl;
pthread_mutex_lock(&mutex);
//当条件不满足时等待
if(count <= 0)
{
cout << "begin wait" << endl;
pthread_cond_wait(&cond,&mutex);
cout << "end wait" << endl;
}
count --;
cout << "in consumer count is " << count << endl;
pthread_mutex_unlock(&mutex);
cout << "consumer release lock" << endl;
return NULL;
}
int main()
{
//两个线程,一个生产者线程一个消费者线程
pthread_t createthread,consumethread;
pthread_create(&consumethread, NULL, consumer, NULL); sleep(2);
pthread_create(&createthread, NULL, creator, NULL);
//主进程等待两个线程结束
pthread_join(createthread, NULL);
pthread_join(consumethread, NULL);
return 0;
}



void * creator(void * arg)
{
int i = 0;
while(i<300)
{
i++;
cout << "creator add lock" << endl;
pthread_mutex_lock(&mutex);
count ++;
cout << "in creator count is : " << count << endl;
if(count > 0)
{
pthread_cond_signal(&cond);
}
cout << "creator release lock" << endl;
pthread_mutex_unlock(&mutex);
}
return NULL;
}
void * consumer(void * arg)
{
int i = 0;
while(i < 100)
{
i++;
cout << "consumer add lock" << endl;
pthread_mutex_lock(&mutex);
if(count <= 0)
{
cout << "begin wait" << endl;
pthread_cond_wait(&cond,&mutex);
cout << "end wait" << endl;
}
count --;
cout << "in consumer count is " << count << endl;
pthread_mutex_unlock(&mutex);
cout << "consumer release lock" << endl;
}
return NULL;
}
int main()
{
pthread_t createthread[2],consumethread[3];
for(int i = 0; i < 3; i++)
{
pthread_create(&consumethread[i], NULL, consumer, NULL);
}
for(int i = 0; i < 2; i++)
{
pthread_create(&createthread[i], NULL, creator, NULL);
}
for(int i = 0; i < 2; i++)
{
pthread_join(createthread[i], NULL);
}
for(int i = 0; i < 3; i++)
{
pthread_join(consumethread[i], NULL);
}
return 0;
}

截取一部分结果截图,可以看出数字是连续变动的,而且
加锁解锁内数字才变动,说明我们对锁和条件变量使用合理
pthread_cond_wait学习笔记的更多相关文章
- js学习笔记:webpack基础入门(一)
之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...
- PHP-自定义模板-学习笔记
1. 开始 这几天,看了李炎恢老师的<PHP第二季度视频>中的“章节7:创建TPL自定义模板”,做一个学习笔记,通过绘制架构图.UML类图和思维导图,来对加深理解. 2. 整体架构图 ...
- PHP-会员登录与注册例子解析-学习笔记
1.开始 最近开始学习李炎恢老师的<PHP第二季度视频>中的“章节5:使用OOP注册会员”,做一个学习笔记,通过绘制基本页面流程和UML类图,来对加深理解. 2.基本页面流程 3.通过UM ...
- 2014年暑假c#学习笔记目录
2014年暑假c#学习笔记 一.C#编程基础 1. c#编程基础之枚举 2. c#编程基础之函数可变参数 3. c#编程基础之字符串基础 4. c#编程基础之字符串函数 5.c#编程基础之ref.ou ...
- JAVA GUI编程学习笔记目录
2014年暑假JAVA GUI编程学习笔记目录 1.JAVA之GUI编程概述 2.JAVA之GUI编程布局 3.JAVA之GUI编程Frame窗口 4.JAVA之GUI编程事件监听机制 5.JAVA之 ...
- seaJs学习笔记2 – seaJs组建库的使用
原文地址:seaJs学习笔记2 – seaJs组建库的使用 我觉得学习新东西并不是会使用它就够了的,会使用仅仅代表你看懂了,理解了,二不代表你深入了,彻悟了它的精髓. 所以不断的学习将是源源不断. 最 ...
- CSS学习笔记
CSS学习笔记 2016年12月15日整理 CSS基础 Chapter1 在console输入escape("宋体") ENTER 就会出现unicode编码 显示"%u ...
- HTML学习笔记
HTML学习笔记 2016年12月15日整理 Chapter1 URL(scheme://host.domain:port/path/filename) scheme: 定义因特网服务的类型,常见的为 ...
- DirectX Graphics Infrastructure(DXGI):最佳范例 学习笔记
今天要学习的这篇文章写的算是比较早的了,大概在DX11时代就写好了,当时龙书11版看得很潦草,并没有注意这篇文章,现在看12,觉得是跳不过去的一篇文章,地址如下: https://msdn.micro ...
随机推荐
- shell脚本批量ssh登陆主机并执行命令
shell脚本批量ssh登陆主机并执行命令 今天在客户现场遇到了这个问题,客户没有管理工具,无法批量登陆主机下发命令,几个个C段啊,让我一个一个登陆,.................. 所以写了个s ...
- sql server 临时表(上) Tempdb概述
一.概述 在sql server里临时表存储在TempDB库中,TempDB是一个系统数据库,它只有Simple恢复模式,也是最小日志记录操作.主要用于存放局部临时表,全局临时表,表变量,都是基于临时 ...
- UITableView编辑模式大全解
1.UITableView 的编辑模式 进入编辑模式 代码体现 // 设置 editing 属性 tableView?.editing = true // 这个设置的时候是有动画效果的 tableVi ...
- 英语背单词app
乐词 √ 真人发音 词根词缀 小组计划及时复习 真人例句 墨墨 单词量测试做的特别好 扇贝 哈哈哈,没用过 百词斩 同样25个单词,我在乐词中背了20分钟,在百词斩中需要60分钟. 原因在于 要记单词 ...
- Boosting Static Representation Robustness for Binary Clone Search against Code Obfuscation and Compiler Optimization
用于理解恶意软件的内部工作原理,并发现系统中的漏洞,逆向工程是一种耗费人工的却很重要的技术.汇编克隆搜索引擎是通过识别那些重复的或者已知的部件来帮助逆向工程师的工作,要想设计健壮的克隆搜索引擎是一项挑 ...
- Ajax 与文件上传
一 Ajax篇 1 ajax简介(Asynchronous Javascript And XML) 异步,Js,XML,即使用Javascript语言与服务器进行异步交互,传输的数据为xml(可扩展标 ...
- python 词云学习
词云入门 三步曲 数据获取:使用爬虫在相关网站上获取文本内容 数据清洗:按一定格式对文本数据进行清洗和提取(文本分类,贴标签) 数据呈现:多维度呈现和解读数据(计算,做表,画图) 一 模块的安装 pi ...
- 【English 】20190319
BOKO鼻子['boʊkoʊ] pores毛孔['pɔ:z] cute漂亮可爱[kjut] DEKO-BOKO pores don't make a girl cute! ideal最理想的[aɪˈ ...
- .NET CORE学习笔记系列(6)——KestrelServer
原文:http://www.cnblogs.com/artech/p/KestrelServer.html 跨平台是ASP.NET Core一个显著的特性,而KestrelServer是目前微软推出了 ...
- uml类图关系
原文地址http://www.jfox.info/uml-lei-tu-guan-xi-fan-hua-ji-cheng-shi-xian-yi-lai-guan-lian-ju-he-zu-he 在 ...