/*
std::condition_variable 提供了两种 wait() 函数。当前线程调用 wait() 后将被阻塞(此时当前线程应该获得了锁(mutex),不妨设获得锁 lck),直到另外某个线程调用 notify_* 唤醒了当前线程。
在线程被阻塞时,该函数会自动调用 lck.unlock() 释放锁,使得其他被阻塞在锁竞争上的线程得以继续执行。另外,一旦当前线程获得通知(notified,通常是另外某个线程调用 notify_* 唤醒了当前线程),wait() 函数也是自动调用 lck.lock(),使得 lck 的状态和 wait 函数被调用时相同。
在第二种情况下(即设置了 Predicate),只有当 pred 条件为 false 时调用 wait() 才会阻塞当前线程,并且在收到其他线程的通知后只有当 pred 为 true 时才会被解除阻塞
*/
#include <iostream>
#include <thread>
#include <mutex>
#include <vector>
#include <condition_variable>
std::vector<int> vec;
std::mutex mu;
std::condition_variable cv;
int countt = ;
bool ready = false;
using namespace std;
void fun(int n)
{
std::unique_lock<std::mutex> loc(mu);
while (!ready)
{
cv.wait(loc);//依照上面的理解:wait操作线程被阻塞,wait操作会将mutex解锁,导致其他的线程都阻塞在这,当其他线程调用notify时,线程解除阻塞,但开始上锁
} int num = ;
while (num--)
{
cout<<n<<"--------"<<num<<endl;
}
} void go()
{
std::unique_lock<std::mutex> lo(mu);
ready = true;
cv.notify_all();//通知所有阻塞的线程进入运行状态
} int main()
{
std::thread th[];
for (int i = ; i < ; ++i)
{
th[i] = std::thread(fun, i);
}
//此时5个线程都开始运行,但都被阻塞住了
go();
for (auto& t:th)
{
t.join();
}
system("pause");
return ;
}

具体参考http://www.cnblogs.com/haippy/p/3252041.html

cv.wait(loc)会解锁,此时阻塞在go函数的线程和fun里面的线程也可以运行,我实在没想通,感觉这个例子举得不好。

std::condition_variable的更多相关文章

  1. C++11 并发指南五(std::condition_variable 详解)

    前面三讲<C++11 并发指南二(std::thread 详解)>,<C++11 并发指南三(std::mutex 详解)>分别介绍了 std::thread,std::mut ...

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

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

  3. 基于std::mutex std::lock_guard std::condition_variable 和std::async实现的简单同步队列

    C++多线程编程中通常会对共享的数据进行写保护,以防止多线程在对共享数据成员进行读写时造成资源争抢导致程序出现未定义的行为.通常的做法是在修改共享数据成员的时候进行加锁--mutex.在使用锁的时候通 ...

  4. std::condition_variable(二)

    #include <iostream> // std::cout #include <thread> // std::thread, std::this_thread::yie ...

  5. 转 C++11 并发指南std::condition_variable详解

    之前看过,但是一直没有怎么用就忘了,转一篇别人的文字记录下来 本文将介绍 C++11 标准中 <condition_variable> 头文件里面的类和相关函数. <conditio ...

  6. std::condition_variable(3)复习

    #include <iostream> // std::cout #include <thread> // std::thread #include <mutex> ...

  7. std::condition_variable(2)复习

    #include <iostream> // std::cout #include <thread> // std::thread, std::this_thread::yie ...

  8. std::condition_variable(复习)

    #include <iostream> // std::cout #include <thread> // std::thread #include <mutex> ...

  9. C++11 并发指南五(std::condition_variable 详解)(转)

    前面三讲<C++11 并发指南二(std::thread 详解)>,<C++11 并发指南三(std::mutex 详解)>分别介绍了 std::thread,std::mut ...

随机推荐

  1. 剩下5种算法代码分析与使用示例(remove 、rotate 、sort、lower_bound、accumulate)

    一.移除性算法 (remove)  C++ Code  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 ...

  2. __packed字节对齐

    比如: typedef __packed struct READ_Command{    u_char code;    u_int addr;    u_char len;} READ_Comman ...

  3. 微信小程序请求wx.request数据,渲染到页面

    先说一下基本使用.官网也有. 比如说你在App.js里面有这些变量.想修改某些值. data: { main_view_bgcolor: "", border: "&qu ...

  4. Mysql时间存储类型优缺点?DATETIME?TIMESTAMP?INT?

    TIMESTAMP 4个字节储存;值以UTC格式保存;.时区转化 ,存储时对当前的时区进行转换,检索时再转换回当前的时区. DATETIME 8个字节储存;实际格式储存;与时区无关;datetime  ...

  5. 字符串函数---itoa()函数具体解释及实现

    itoa()函数 itoa():char *itoa( int value, char *string,int radix); 原型说明: value:欲转换的数据. string:目标字符串的地址. ...

  6. shell监控脚本实例—监控mysql主从复制

    分享一例shell脚本,用于监测mysql数据库的主从复制,有需要的朋友不妨参考学习下. 转自:http://www.jbxue.com/article/14103.html(转载请注明出处) 本节内 ...

  7. 【原创】关于$test$plusargs和$value$plusargs的小结【SystemVerilog/Verilog】

    [原创]关于$test$plusargs和$value$plusargs的小结 Abtract $test$plusargs和$value$plusargs作为进行Verilog和SystemVeri ...

  8. win32之取画刷的方法

    取画刷(HBRUSH) 的六种方法2009-- :00HBRUSH hbr; 第一种: hbr= CreateSolidBrush(RGB(,,)); //单色的画刷 第二种: hbr= (HBRUS ...

  9. jQuery瀑布流插件 Masonry

    http://www.jq22.com/yanshi362 参考案例 http://image.quanjing.com/lvyou/

  10. filebeat+kafka失败

    filebeat端配置 #----------------------------- Kafka output -------------------------------- output.kafk ...