多线程情况下,往往需要使用互斥变量来实现线程间的同步,实现资源正确共享。

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(&notempty, NULL);
pthread_cond_init(&notfull, NULL);
}
void push(int i);
int pop();
~MyQueue(){
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&notempty);
pthread_cond_destroy(&notfull);
} 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(&notfull, &mutex);
}
buf[tail] = i;
tail = (tail + 1) % SIZE;
//通知等待notempty的线程,队列不空
pthread_mutex_unlock(&mutex);
pthread_cond_signal(&notempty);
} //头部取元素
int MyQueue::pop() {
pthread_mutex_lock(&mutex);
while (is_empty()) {
cout << "queue is empty, wait" << endl;
pthread_cond_wait(&notempty, &mutex);
}
int i = buf[head];
head = (head + 1) % SIZE;
//通知等待notfull的线程,队列不满
pthread_mutex_unlock(&mutex);
pthread_cond_signal(&notfull);
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-条件变量的使用(线程安全队列的实现)的更多相关文章

  1. linux多线程同步pthread_cond_XXX条件变量的理解

    在linux多线程编程中,线程的执行顺序是不可预知的,但是有时候由于某些需求,需要多个线程在启动时按照一定的顺序执行,虽然可以使用一些比较简陋的做法,例如:如果有3个线程 ABC,要求执行顺序是A-- ...

  2. Linux多线程编程的条件变量

    在stackoverflow上看到一关于多线程条件变量的问题,题主问道:什么时候会用到条件变量,mutex还不够吗?有个叫slowjelj的人做了很好的回答,我再看这个哥们其他话题的一些回答,感觉水平 ...

  3. Linux 多线程编程—使用条件变量实现循环打印

    编写一个程序,开启3个线程,这3个线程的ID分别为A.B.C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示:如:ABCABC….依次递推. 使用条件变量来实现: #inc ...

  4. C++并发编程 条件变量 condition_variable,线程安全队列示例

    1. 背景 c++11中提供了对线程与条件变量的更好支持,对于写多线程程序方便了很多. 再看c++并发编程,记一下学习笔记. 2. c++11 提供的相关api 3.1 wait wait用于无条件等 ...

  5. Linux同步机制(二) - 条件变量,信号量,文件锁,栅栏

    1 条件变量 条件变量是一种同步机制,允许线程挂起,直到共享数据上的某些条件得到满足. 1.1 相关函数 #include <pthread.h>  pthread_cond_t cond ...

  6. Linux互斥锁、条件变量和信号量

    Linux互斥锁.条件变量和信号量  来自http://kongweile.iteye.com/blog/1155490 http://www.cnblogs.com/qingxia/archive/ ...

  7. linux 互斥锁和条件变量

    为什么有条件变量? 请参看一个线程等待某种事件发生 注意:本文是linux c版本的条件变量和互斥锁(mutex),不是C++的. mutex : mutual exclusion(相互排斥) 1,互 ...

  8. linux网络编程-posix条件变量(40)

    举一个列子来说明条件变量: 假设有两个线程同时访问全局变量n,初始化值是0, 一个线程进入临界区,进行互斥操作,线程当n大于0的时候才执行下面的操作,如果n不大于0,该线程就一直等待. 另外一个线程也 ...

  9. linux C++ 多线程使用pthread_cond 条件变量

    1. 背景 多线程中经常需要使用到锁(pthread_mutex_t)来完成多个线程之间的互斥操作. 但是互斥锁有一个明显到缺点: 只有两种状态,锁定和非锁定. 而条件变量则通过允许线程阻塞并等待另一 ...

随机推荐

  1. mysql:添加索引

    ALTER TABLE tb_user_type ADD INDEX user_type_index3 (report_type_id) ALTER TABLE tb_user_type ADD IN ...

  2. 1214 - Large Division -- LightOj(大数取余)

    http://lightoj.com/volume_showproblem.php?problem=1214 这就是一道简单的大数取余. 还想还用到了同余定理: 所谓的同余,顾名思义,就是许多的数被一 ...

  3. 在线编辑器的使用-KindEditor

    第一种:KindEditor编辑器 步骤一:加载相应的核心的文件 下载地址:http://kindeditor.net/demo.php <link rel="stylesheet&q ...

  4. [django]在virtualenv下安装的第三方库的使用方法

    在virtualenv下安装的第三方库,例如south, requests等,如果想在django中使用,需要先将库添加到settings.py的INSTALLED_APPS中, 以south, re ...

  5. VS2013无调试信息

    Debug模式,运行时完全正常,但是一调试就出现对话框,显示出错信息: "无法找到"XXX.exe"的调试信息,或者调试信息不匹配.未使用调试信息生成二进制文件.&quo ...

  6. multiple definition of `err_sys' 《UNIX环境高级编程》

    本文地址:http://www.cnblogs.com/yhLinux/p/4079930.html 问题描述: [点击此处直接看解决方案] 在练习<UNIX环境高级编程>APUE程序清单 ...

  7. EditPlus 3.1

    User:GNU Serial:918A8-20DD8-44ZA1-B0W4A-13T66

  8. [转]JavaScript字符串函数大全

    JS自带函数concat将两个或多个字符的文本组合起来,返回一个新的字符串.var a = "hello";var b = ",world";var c = a ...

  9. Inno setup中定制安装路径

    我的程序修改了安装界面,所以我的界面中提供了更改安装路径的方法. 用户修改后的路径会被传回inno setup脚本,脚本中需要做的事情如下: 1,写一个函数,来返回新的安装路径,如: function ...

  10. 【解题报告】BZOJ2550: [Ctsc2004]公式编辑器

    题意:给定一个可视化计算器的操作序列,包括插入数字.字母.运算符.分数.矩阵以及移动光标.矩阵插入行.插入列,输出操作序列结束后的屏显(数学输出). 解法:这题既可以用来提升OI/ACM写大代码模拟题 ...