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

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. APP审核被拒,原因总结

    今天早上,突然看到上周末提交的APP,审核被拒了.原以为是因为IPV6审核没过,后来查看原因后发现是,app的3张展示图里面,有些内容显示的有:测试XX等字眼.苹果说提交的版本不能是含有 test,t ...

  2. Windows 下 ffmpeg 转 mp4

    最近在研究所有视频格式转  mp4 因为html5 只支持mov MP4 等格式..查阅了 很多资料发现  转成flv  很简单.. 可是要转 mp4 就难了... 经过我不屑的努力..终于转换成功了 ...

  3. 51nod 1138 连续整数的和(数学公式)

    1138 连续整数的和 #include <iostream> #include <cmath> #include <cstdio> using namespace ...

  4. 更改 Skype for Business Online 的 Sip 地址以匹配UPN

    var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...

  5. SQL Server 分区表

    分区表可以提高查询效率 但是如果是分区表的话,表数据就会按照你指定的规则分放到不同的文件里,把一个大的数据文件拆分为多个小文件,还可以把这些小文件放在不同的磁盘下由多个cpu进行处理.这样文件的大小随 ...

  6. 《python核心编程》读书笔记——列表解析

    列表解析是列表类型的方法,这种方法结合了列表的方括弧.for循环.if语句. 用for把处理后的值放入列表: squared = [ x**2 for x in range(4) ] for i in ...

  7. Excel公式设置单元格颜色

    Excel2010 “条件格式"-"新建规则"-"使用公式确定要设置格式的单元格" 公式如下: =OR(H2<=-20%,H2>=20%, ...

  8. linux 第一题 计算题

    #!/bin/bash echo "输入第一个数字" read A b= ]] do && [[ ${A} != *[!]* ]] then echo " ...

  9. :尝试加载 Oracle 客户端库时引发 BadImageFormatException。如果在安装 32 位 Oracle 客户端组件的情况下以 64 位模式运行,将出现此问题。 ---> System.BadImageFormatException: 试图加载格式不正确的程序。

    iis有程序池有一个属性  是否启用32位应用程序默认是true的特别注意windows2008 服务器系统iis默认的是true 这样发布一下也是必须要做的

  10. swift的运算符

    1.什么是运算符?它有什么作用? 运算符是一种特定的符号或者表达式.它用来验证.修改.合并变量. 2.运算符有哪些? 运算符有很多,很多朋友学的很烦.这里我依据它的作用把它分为几块来介绍: a:赋值运 ...