Linux多线程系列-2-条件变量的使用(线程安全队列的实现)
多线程情况下,往往需要使用互斥变量来实现线程间的同步,实现资源正确共享。
linux下使用如下变量和函数
//条件变量
pthread_cond_t
int pthread_cond_init (pthread_cond_t *c, const pthread_condattr_t *a)
int pthread_cond_wait (pthread_cond_t *c, pthread_mutex_t *m)
int pthread_cond_timedwait (pthread_cond_t *c,pthread_mutex_t *m, const struct timespec *t)
int pthread_cond_signal (pthread_cond_t *c)
int pthread_cond_destroy (pthread_cond_t *c) //互斥变量
pthread_mutex_t
int pthread_mutex_init (pthread_mutex_t *m, const pthread_mutexattr_t *a)
int pthread_mutex_lock(pthread_mutex_t *m)
int pthread_mutex_unlock(pthread_mutex_t *m)
int pthread_mutex_destroy (pthread_mutex_t *m)
利用条件变量和互斥变量来实现一个互斥队列
#include <pthread.h>
const int SIZE = 100;
const int NUM = 200;
class MyQueue{
public:
MyQueue():head(0),tail(0){
//变量初始化
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(¬empty, NULL);
pthread_cond_init(¬full, NULL);
}
void push(int i);
int pop();
~MyQueue(){
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(¬empty);
pthread_cond_destroy(¬full);
} private:
bool is_full() {
return (tail + 1) % SIZE == head;
}
bool is_empty() {
return tail == head;
} private:
int buf[SIZE];
int head;
int tail;
pthread_mutex_t mutex; //互斥使用锁
pthread_cond_t notempty; //非空条件变量
pthread_cond_t notfull; //队列不满条件变量 }; //尾部插入元素
void MyQueue::push(int i) {
pthread_mutex_lock(&mutex);
while (is_full()) {
cout << "queue is full, wait" << endl;
pthread_cond_wait(¬full, &mutex);
}
buf[tail] = i;
tail = (tail + 1) % SIZE;
//通知等待notempty的线程,队列不空
pthread_mutex_unlock(&mutex);
pthread_cond_signal(¬empty);
} //头部取元素
int MyQueue::pop() {
pthread_mutex_lock(&mutex);
while (is_empty()) {
cout << "queue is empty, wait" << endl;
pthread_cond_wait(¬empty, &mutex);
}
int i = buf[head];
head = (head + 1) % SIZE;
//通知等待notfull的线程,队列不满
pthread_mutex_unlock(&mutex);
pthread_cond_signal(¬full);
return i;
} void* producer(void* args) {
MyQueue* queue = (MyQueue*)args;
for (int i = 0; i < NUM; ++i) {
queue->push(i);
cout << "push:" << i << endl;
}
}
void* consumer(void* args) {
MyQueue* queue = (MyQueue*)args;
for (int i = 0; i < NUM; ++i) {
cout << "pop:" << queue->pop() << endl;
}
}
int main() {
MyQueue queue; pthread_t pro_thread;
pthread_t con_thread;
pthread_create(&pro_thread, NULL, producer, &queue);
pthread_create(&con_thread, NULL, consumer, &queue); pthread_join(pro_thread, NULL);
pthread_join(con_thread, NULL);
cout << "End" << endl;
}
Linux多线程系列-2-条件变量的使用(线程安全队列的实现)的更多相关文章
- linux多线程同步pthread_cond_XXX条件变量的理解
在linux多线程编程中,线程的执行顺序是不可预知的,但是有时候由于某些需求,需要多个线程在启动时按照一定的顺序执行,虽然可以使用一些比较简陋的做法,例如:如果有3个线程 ABC,要求执行顺序是A-- ...
- Linux多线程编程的条件变量
在stackoverflow上看到一关于多线程条件变量的问题,题主问道:什么时候会用到条件变量,mutex还不够吗?有个叫slowjelj的人做了很好的回答,我再看这个哥们其他话题的一些回答,感觉水平 ...
- Linux 多线程编程—使用条件变量实现循环打印
编写一个程序,开启3个线程,这3个线程的ID分别为A.B.C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示:如:ABCABC….依次递推. 使用条件变量来实现: #inc ...
- C++并发编程 条件变量 condition_variable,线程安全队列示例
1. 背景 c++11中提供了对线程与条件变量的更好支持,对于写多线程程序方便了很多. 再看c++并发编程,记一下学习笔记. 2. c++11 提供的相关api 3.1 wait wait用于无条件等 ...
- Linux同步机制(二) - 条件变量,信号量,文件锁,栅栏
1 条件变量 条件变量是一种同步机制,允许线程挂起,直到共享数据上的某些条件得到满足. 1.1 相关函数 #include <pthread.h> pthread_cond_t cond ...
- Linux互斥锁、条件变量和信号量
Linux互斥锁.条件变量和信号量 来自http://kongweile.iteye.com/blog/1155490 http://www.cnblogs.com/qingxia/archive/ ...
- linux 互斥锁和条件变量
为什么有条件变量? 请参看一个线程等待某种事件发生 注意:本文是linux c版本的条件变量和互斥锁(mutex),不是C++的. mutex : mutual exclusion(相互排斥) 1,互 ...
- linux网络编程-posix条件变量(40)
举一个列子来说明条件变量: 假设有两个线程同时访问全局变量n,初始化值是0, 一个线程进入临界区,进行互斥操作,线程当n大于0的时候才执行下面的操作,如果n不大于0,该线程就一直等待. 另外一个线程也 ...
- linux C++ 多线程使用pthread_cond 条件变量
1. 背景 多线程中经常需要使用到锁(pthread_mutex_t)来完成多个线程之间的互斥操作. 但是互斥锁有一个明显到缺点: 只有两种状态,锁定和非锁定. 而条件变量则通过允许线程阻塞并等待另一 ...
随机推荐
- python day2 字符串的方法
1.首字母大写 name = "wuyuchao"result = name.capitalize()print(result)返回 Wuyuchao--------------- ...
- Scatterplots 散点图
Simple Scatterplot # Simple Scatterplot attach(mtcars)plot(wt, mpg, main="Scatterplot Example&q ...
- Windows Desktop 调用 WinRT api
<Reference Include="Windows"> <HintPath>..\..\..\..\..\..\Program Files (x86)\ ...
- Codeforces Round #200 (Div. 1) D. Water Tree(dfs序加线段树)
思路: dfs序其实是很水的东西. 和树链剖分一样, 都是对树链的hash. 该题做法是:每次对子树全部赋值为1,对一个点赋值为0,查询子树最小值. 该题需要注意的是:当我们对一棵子树全都赋值为1的 ...
- 一个书店管理系统java
自己的第一个小程序 ps:书是在集合里面后面文件处理的有一点小问题,希望有人会给点意见 //客户类 import java.io.Serializable; public class Customer ...
- H5前端性能测试快速入门
前言 说到H5测试,对于做WEB测试的同学来说再熟悉不过了,它包括页H5功能测试,前端性能测试,浏览器兼容性能测试,以及服务端性能测试.那本文谈到的则是H5前端性能测试,并希望通过阅读本文后,能够知道 ...
- SortedDictionary
对一个Dictionary<TKey, TValue>进行键排序可以直接用SortedDictionary SortedDictionary<TKey, TValue> 泛型类 ...
- js中replace的回调函数使用。
这只是一个小问题,但是之前并没有发现.这个问题就是replace的第二个函数是支持回调函数的. var ext = new RegExp('f','g'); 1.str.replace(ext ,1) ...
- INotifyPropertyChanged接口的PropertyChanged 事件
INotifyPropertyChanged 接口用于向客户端(通常是执行绑定的客户端)发出某一属性值已更改的通知. 例如,考虑一个带有名为 FirstName 属性的 Person 对象. 若要提供 ...
- CSS3多列
CSS3多列,通过创建列对页面内容进行布局,轻松实现类似Pinterest.花瓣的瀑布流式布局. 主要包含: column-count 列数 column-gap 列间宽度 column-r ...