C++11 条件变量
C++11中的条件变量提供了用户等待的同步机制,在同步队列的应用中有很大的便利。
简单同步队列代码如下(SimpleSyncQueue.h):
#ifndef SIMPLESYNCQUEUE_H
#define SIMPLESYNCQUEUE_H #include <thread>
#include <condition_variable>
#include <mutex>
#include <list>
#include <iostream> using namespace std; template<typename T>
class SimpleSyncQueue
{
public:
SimpleSyncQueue()
{ } void Put(const T& x)
{
//调用Put函数的线程获取互斥量,如果互斥量正在被占用,将等待
std::lock_guard<std::mutex> lk(m_mutex); //保存数据
m_queue.push_back(x); //通知等待m_notEmpty的线程
m_notEmpty.notify_one();
} void Take(T& x)
{
//调用Take函数的线程获取互斥量,如果互斥量正在被占用,将等待
std::unique_lock<std::mutex> locker(m_mutex); m_notEmpty.wait(locker,[this] { if(m_queue.empty())
{
cout<<"现在队列为空,请稍等"<<endl;
} return !m_queue.empty();
}); x = m_queue.front();
m_queue.pop_front();
} bool Empty()
{
std::lock_guard<std::mutex> lk(m_mutex); return m_queue.empty();
} size_t Size()
{
std::lock_guard<std::mutex> lk(m_mutex); return m_queue.size();
} private:
std::list<T> m_queue; /* 用于等待的同步变量 */
std::mutex m_mutex; /* 用于多线程同步的互斥量 */
std::condition_variable m_notEmpty; /* 用于等待的同步变量 */
}; #endif // SIMPLESYNCQUEUE_H
测试代码如下:
#include <QCoreApplication> #include "simplesyncqueue.h" SimpleSyncQueue<int> testQueue;
bool threadrun = true; void consumerthread()
{
int get; while(threadrun)
{
testQueue.Take(get); cout<<"consumer thread get: "<<get<<endl;
} cout<<"consumerthread exit"<<endl;
} void producerthread()
{
char in = ; while(threadrun)
{
cin>>in; cout<<"user input: "<<in<<endl; switch(in)
{
case 'p': testQueue.Put((int)); break;
case 'q': threadrun = false; break;
default: break;
}
} cout<<"producerthread exit"<<endl;
} int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv); std::thread t1(consumerthread); std::thread t2(producerthread);
t1.detach(); t2.join(); cout<<"program exit"<<endl; return a.exec();
}
测试结果如下:

看看condition_variable类中的wait函数定义,参见http://en.cppreference.com/w/cpp/thread/condition_variable/wait
关于这个函数,其实疑惑的是pred ()函数会被执行多少遍的问题。cppreference里的详解里给了一个 Equivalent to:
while (!pred()) {
wait(lock);
}
从Equivalent To可以看到pred开始被执行一次。
C++11 条件变量的更多相关文章
- c++11の条件变量
一.条件变量的引入 std::condition_variable 解决了死锁并且控制的资源的访问顺序二避免不必要的等待.当互斥操作不够用而引入的.比如,线程可能需要等待某个条件为真才能继续执行,而一 ...
- c++11 条件变量 生产者-消费者 并发线程
http://baptiste-wicht.com/posts/2012/04/c11-concurrency-tutorial-advanced-locking-and-condition-vari ...
- C++11 中的线程、锁和条件变量
转自:http://blog.jobbole.com/44409/ 线程 类std::thread代表一个可执行线程,使用时必须包含头文件<thread>.std::thread可以和普通 ...
- C++11并发——多线程条件变量std::condition_variable(四)
https://www.jianshu.com/p/a31d4fb5594f https://blog.csdn.net/y396397735/article/details/81272752 htt ...
- c++11中的线程、锁和条件变量
void func(int i, double d, const string& s) { cout << i << ", " << d ...
- C++11并行编程-条件变量(condition_variable)详细说明
<condition_variable >头文件主要包含有类和函数相关的条件变量. 包括相关类 std::condition_variable和 std::condition_variab ...
- Windows:C++11并发编程-条件变量(condition_variable)详解
<condition_variable >头文件主要包含了与条件变量相关的类和函数.相关的类包括 std::condition_variable和 std::condition_varia ...
- APUE学习笔记——11 线程同步、互斥锁、自旋锁、条件变量
线程同步 同属于一个进程的不同线程是共享内存的,因而在执行过程中需要考虑数据的一致性. 假设:进程有一变量i=0,线程A执行i++,线程B执行i++,那么最终i的取值是多少呢?似乎一定 ...
- c++11多线程记录6:条件变量(condition variables)
https://www.youtube.com/watch?v=13dFggo4t_I视频地址 实例1 考虑这样一个场景:存在一个全局队列deque,线程A向deque中推入数据(写),线程B从deq ...
随机推荐
- USACO Party Lamps
题目大意:一排灯有n个,有4种开关,每种开关能改变一些灯现在的状态(亮的变暗,暗的变亮)现在已知一些灯的亮暗情况,问所以可能的情况是哪些 思路:同一种开关开两次显然是没效果的,那么枚举每个开关是否开就 ...
- BZOJ2246 [SDOI2011]迷宫探险 【记忆化搜索dp + 概率】
题目 输入格式 输出格式 仅包含一个数字,表示在执行最优策略时,人物活着走出迷宫的概率.四舍五入保留3位小数. 输入样例 4 3 3 2 .$. A#B A#C @@@ 143 37 335 85 9 ...
- Lucky and Good Months by Gregorian Calendar(poj 3393)
大致题意: 科普文一篇,文章80%都是无用信息,因为都是常识,但是又不得不看,因为有20%是常人不知道的历史常识. 定义: Goog month : 该月第一个工作日为星期一的月份 Luckly mo ...
- 玩转css样式选择器----利用padding实现元素等比缩放
- 汉若塔系列续:汉诺塔VIII、汉诺塔IX、汉诺塔X。
汉诺塔VIII,在经典汉若塔问题上,问n个盘子的情况下,移动m次以后,是什么状态.(与第七代互为逆命题) 我的思路:本质还是dfs,但是用m的值来指引方向,每搜一层确定第i个盘子在哪个塔,o(n)的算 ...
- Influx kafka
http://www.opscoder.info/kafka-influxdb.html
- php 笔记 汇总 学习
php命令行:通过命令行进入到当前要被执行的php文件路径,然后输入php环境可执行路径(后面包含php.exe),然后输入要被执行的php文件,比如runData.php即可. php框架:yaf. ...
- python type
基于2.7 版本 type 是内置函数,有两种用法 class type(object) With one argument, return the type of an object. The re ...
- java消息队列怎么用
消息队列的使用场景是怎样的? 经常听到诸如rebbitmq,activemq,请教一下各位前辈消息队列的使用场景是怎样的,什么时候会用到它 校验用户名等信息,如果没问题会在数据库中添加一个用户记录 ...
- 基于centos 创建stress镜像——源码安装stress
上一篇文章进行了yum安装stress,这次对stress进行源码编译安装,并且生成新的镜像 创建Dockerfile目录 [vagrant@localhost ~]$ mkdir -p /tmp/s ...