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

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. RPLIDAR使用和测试

    采购的RPLIDAR刚刚到货,先拆封,内部包括雷达主机.线缆.串口USB转接.USB线.   根据说明在此处下载驱动和SDK,雷达的数据实际是通过串口输出的,所以驱动只是针对串口USB转换芯片CP21 ...

  2. win7环境下安装运行gotour【转载整理】

    转载请注明出处:http://www.cnblogs.com/Vulpers/p/5562586.html 最近尝试学习golang,在某个网站(真忘了)上发现gotour是一款灰常叼的教程& ...

  3. 关于webstorm 对 vue的设置

    1. 首先安装vue插件,安装方法: setting  -->  plugin  ,点击plugin,在内容部分的左侧输入框输入vue,会出现两个关于vue的插件,点击安装即可.安装完成后,就可 ...

  4. [C#.NET]

    Control.Refresh - does an Control.Invalidate followed by Control.Update. Refresh: 强制控件使其工作区无效并立即重绘自己 ...

  5. Dancing Links & Algorithm X

    1 参考链接 http://www.cnblogs.com/steady/archive/2011/03/15/1984791.html#undefined http://en.wikipedia.o ...

  6. Android Sqlite数据库相关——实现 Sqlite 数据库版本升级

    继承SQLiteOpenHelper类后,实现其中的onUpgrade 方法 @Override public void onUpgrade(SQLiteDatabase db, int oldVer ...

  7. Cocostudio 文章列表

    Cocostudio 文章列表 Cocostudio(1) 容器层的使用- ScrollView ListView PageViewhttp://www.cnblogs.com/TS-qrt/arti ...

  8. Spring MVC 流程图(转)

    Spring MVC工作流程图   图一   图二    Spring工作流程描述       1. 用户向服务器发送请求,请求被Spring 前端控制Servelt DispatcherServle ...

  9. ASP.NET Web服务调用发生错误,错误代码404

    现象: iOS端使用ASIHTTP连接Web服务时,得到的数据是一个错误代码为404的页面,错误信息(web.config添加<customErrors mode="Off" ...

  10. gridview汇出EXCEL (ExportGridViewToExcel(dt, HttpContext.Current.Response);)

    调用 ExportGridViewToExcel(dt, HttpContext.Current.Response); private void ExportGridViewToExcel(DataT ...