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 条件变量的更多相关文章

  1. c++11の条件变量

    一.条件变量的引入 std::condition_variable 解决了死锁并且控制的资源的访问顺序二避免不必要的等待.当互斥操作不够用而引入的.比如,线程可能需要等待某个条件为真才能继续执行,而一 ...

  2. c++11 条件变量 生产者-消费者 并发线程

    http://baptiste-wicht.com/posts/2012/04/c11-concurrency-tutorial-advanced-locking-and-condition-vari ...

  3. C++11 中的线程、锁和条件变量

    转自:http://blog.jobbole.com/44409/ 线程 类std::thread代表一个可执行线程,使用时必须包含头文件<thread>.std::thread可以和普通 ...

  4. C++11并发——多线程条件变量std::condition_variable(四)

    https://www.jianshu.com/p/a31d4fb5594f https://blog.csdn.net/y396397735/article/details/81272752 htt ...

  5. c++11中的线程、锁和条件变量

    void func(int i, double d, const string& s) { cout << i << ", " << d ...

  6. C++11并行编程-条件变量(condition_variable)详细说明

    <condition_variable >头文件主要包含有类和函数相关的条件变量. 包括相关类 std::condition_variable和 std::condition_variab ...

  7. Windows:C++11并发编程-条件变量(condition_variable)详解

    <condition_variable >头文件主要包含了与条件变量相关的类和函数.相关的类包括 std::condition_variable和 std::condition_varia ...

  8. APUE学习笔记——11 线程同步、互斥锁、自旋锁、条件变量

    线程同步     同属于一个进程的不同线程是共享内存的,因而在执行过程中需要考虑数据的一致性.     假设:进程有一变量i=0,线程A执行i++,线程B执行i++,那么最终i的取值是多少呢?似乎一定 ...

  9. c++11多线程记录6:条件变量(condition variables)

    https://www.youtube.com/watch?v=13dFggo4t_I视频地址 实例1 考虑这样一个场景:存在一个全局队列deque,线程A向deque中推入数据(写),线程B从deq ...

随机推荐

  1. BZOJ 4753 [Jsoi2016]最佳团体 ——01分数规划 树形DP

    要求比值最大,当然用分数规划. 二分答案,转化为选取一个最大的联通块使得它们的和大于0 然后我们直接DP. 复杂度$O(n^2\log {n})$ #include <map> #incl ...

  2. [BZOJ2393] Cirno的完美算数教室(dfs+容斥原理)

    传送门 先通过dfs预处理出来所有只有2和9的数,也就大概2000多个. 想在[L,R]中找到是这些数的倍数的数,可以通过容斥原理 那么如果a % b == 0,那么便可以把 a 去掉,因为 b 的倍 ...

  3. 【扫描线或树状数组】CSU 1335 高桥和低桥

    http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1335 [题意] 给定n座桥的高度,给定m次洪水每次的涨水水位ai和退水水位bi 询问有多少座桥 ...

  4. AtCoder Regular Contest 074F - Lotus Leaves

    $n \leq 300,m \leq 300$,$n*m$的格子里有起点有终点有空地有障碍,人会从起点选一个同行或同列空地跳过去,然后一直这样跳到终点.求至少删掉多少格子使得人跳不到终点. 首先S和T ...

  5. 在Fedora 22下安装配置RealVNC Server 5.2.3的经验总结

    RealVNC是目前功能最全.性能最好的VNC商业软件套件,很多时候为了确保性能和功能的统一,还是大量地在使用RealVNC.最近在Fedora 22工作站上安装RealVNC Server 5.2. ...

  6. 安装破解版的webstorne

    参考以下链接:https://www.cnblogs.com/cui-cui/p/8507435.html

  7. Fast I/O 模板

    [来源:2017 Multi-University Training Contest - Team 1] //面包有毒:P #define BUF_SIZE 100000 //fread -> ...

  8. 小窥React360——用React创建360全景VR体验

    前言    混迹VR届的发烧友兼开发者们一定不要错过这款FaceBook推出的跨端VR开发框架——React360,称为360全景体验框架更为准确,因为其前身是FaceBook和Oculus2017年 ...

  9. 关于Properties 集 添加数据 遍历数据

    public static void fun1(){ Properties v = new Properties(); v.setProperty("a","1" ...

  10. Info.plist 的字段解释

    bundle字段 这些字段名都是XML中的名称,在xcode的属性编辑器中,名字并不相同 bundle目录中的属性列表详细描述了有关该bundle的信息.Finder和一些系统API在一些情况下会使用 ...