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 ...
随机推荐
- 洛谷P2522 - [HAOI2011]Problem b
Portal Description 进行\(T(T\leq10^5)\)次询问,每次给出\(x_1,x_2,y_1,y_2\)和\(d\)(均不超过\(10^5\)),求\(\sum_{i=x_1} ...
- Linux命令——top
top命令可以实时动态地查看系统的整体运行情况,是一个综合了多方信息监测系统性能和运行信息的实用工具.通过top命令所提供的互动式界面,用热键可以管理. 语法 top(选项) 选项 -b:以批处理模式 ...
- 转:WOM 编码与一次写入型存储器的重复使用
转自:WOM 编码与一次写入型存储器的重复使用 (很有趣的算法设计)——来自 Matrix67: The Aha Moments 大神 计算机历史上,很多存储器的写入操作都是一次性的. Wikiped ...
- 【C++】DLL内共享数据区在进程间共享数据(重要)
因项目需要,需要在DLL中共享数据,即DLL中某一变量只执行一次,在运行DLL中其他函数时该变量值不改变:刚开始想法理解错误,搜到了DLL进程间共享数据段,后面发现直接在DLL中定义全局变量就行,当时 ...
- 湘潭大学oj 1206 Dormitory's Elevator dp
27153 njczy2010 1206 Accepted 1976 KB 234 MS G++ 1415 B 2014-09-28 10:01:23 真是吐血ac,,,,这么easy的题..... ...
- R语言入门-画图(二)--heatmap
一.函数参数: pheatmap参数: treeheight_row #横有多长 treeheight_col #竖有多长 cluster_cols=FLASE #单一方向聚类,也就是只有一边有树状结 ...
- AC日记——[USACO08DEC]干草出售Hay For Sale 洛谷 P2925
题目描述 Farmer John suffered a terrible loss when giant Australian cockroaches ate the entirety of his ...
- python文件追加及时间获取
一.python:文件的读取.创建.追加.删除.清空 2011-10-24 11:36:35| 分类: python |举报 |字号 订阅 一.用Python创建一个新文件,内容是从0到9的整数 ...
- ByteArrayInputStream的作用,和BufferedOutputStream 的区别
个人好奇ByteArrayInputStream,到底是有什么用于是百度了一些资料 整合了下,********这两个类对于要创建临时性文件的程序以及网络数据的传输.数据压缩后的传输等可以提高运行的的效 ...
- 用canvas实现一个colorpicker
http://www.cnblogs.com/ufex/p/6382982.html 每个浏览器都有自己的特点,比如今天要做的colorpicker就是,一千个浏览器,一千个哈姆雷特,一千个color ...