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 ...
随机推荐
- Prometheus Operator 架构 - 每天5分钟玩转 Docker 容器技术(178)
本节讨论 Prometheus Operator 的架构.因为 Prometheus Operator 是基于 Prometheus 的,我们需要先了解一下 Prometheus. Prometheu ...
- C#中Skip和Take的用法
Skip()和Take()方法都是IEnumerable<T> 接口的扩展方法,包括C#中的所有Collections类,如ArrayList,Queue,Stack等等,还有数组和字符串 ...
- asp.net网页上获取其中表格中的数据(爬数据)
下面的方法获取页面中表格数据,每个页面不相同,获取的方式(主要是正则表达式)不一样,只是提供方法参考.大神勿喷,刚使用了,就记下来了. 其中数据怎么存,主要就看着怎么使用了.只是方便记录就都放在lis ...
- CVE-2018-8120 分析
目录 CVE-2018-8120 分析 1.实验环境 1.1.操作系统 1.2.用到的分析工具 2.假如 2.1.我想提权 2.2. 有一个处于内核空间,极少被调用的函数 2.3.R3任意修改R0地址 ...
- 【PAT】B1018 锤子剪刀布
抄的柳婼小姐姐的,感觉三个数求最大那里用的真棒 #include <stdio.h> int main() { int N; scanf("%d", &N); ...
- VMware Converter Standalone支持Unix系统吗?
支持的客户操作系统: Windows Vista SP2 (32-bit and 64-bit) Windows Server 2008 SP2 (32-bit and 64-bit) Windows ...
- Gitlab利用Webhook实现Push代码后的jenkins自动构建
之前部署了Gitlab的代码托管平台和Jenkins的代码发布平台.通常是开发后的代码先推到Gitlab上管理,然后在Jenkins里通过脚本构建代码发布.这种方式每次在发版的时候,需要人工去执行je ...
- 如何用Nginx解决前端跨域问题?
前言 在开发静态页面时,类似Vue的应用,我们常会调用一些接口,这些接口极可能是跨域,然后浏览器就会报cross-origin问题不给调. 最简单的解决方法,就是把浏览器设为忽略安全问题,设置--di ...
- 详解Linux双网卡绑定之bond0
1.什么是bond? 网卡bond是通过多张网卡绑定为一个逻辑网卡,实现本地网卡的冗余,带宽扩容和负载均衡,在生产场景中是一种常用的技术.Kernels 2.4.12及以后的版本均供bonding模块 ...
- 跳跳棋[LCA+二分查找]-洛谷1852
传送门 这真是一道神仙题 虽然我猜到了这是一道LCA的题 但是... 第一遍看题,我是怎么也没想到能和树形图扯上关系 并且用上LCA 但其实其实和上一道lightoj上的那道题很类似 只不过那时一道很 ...