std::condition_variable
/*
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的更多相关文章
- C++11 并发指南五(std::condition_variable 详解)
前面三讲<C++11 并发指南二(std::thread 详解)>,<C++11 并发指南三(std::mutex 详解)>分别介绍了 std::thread,std::mut ...
- C++11并发——多线程条件变量std::condition_variable(四)
https://www.jianshu.com/p/a31d4fb5594f https://blog.csdn.net/y396397735/article/details/81272752 htt ...
- 基于std::mutex std::lock_guard std::condition_variable 和std::async实现的简单同步队列
C++多线程编程中通常会对共享的数据进行写保护,以防止多线程在对共享数据成员进行读写时造成资源争抢导致程序出现未定义的行为.通常的做法是在修改共享数据成员的时候进行加锁--mutex.在使用锁的时候通 ...
- std::condition_variable(二)
#include <iostream> // std::cout #include <thread> // std::thread, std::this_thread::yie ...
- 转 C++11 并发指南std::condition_variable详解
之前看过,但是一直没有怎么用就忘了,转一篇别人的文字记录下来 本文将介绍 C++11 标准中 <condition_variable> 头文件里面的类和相关函数. <conditio ...
- std::condition_variable(3)复习
#include <iostream> // std::cout #include <thread> // std::thread #include <mutex> ...
- std::condition_variable(2)复习
#include <iostream> // std::cout #include <thread> // std::thread, std::this_thread::yie ...
- std::condition_variable(复习)
#include <iostream> // std::cout #include <thread> // std::thread #include <mutex> ...
- C++11 并发指南五(std::condition_variable 详解)(转)
前面三讲<C++11 并发指南二(std::thread 详解)>,<C++11 并发指南三(std::mutex 详解)>分别介绍了 std::thread,std::mut ...
随机推荐
- Mosquitto----服务器日志
客户端连接日志 1403334375: New connection from 121.201.8.163 on port 1883. 1403334375: New client connected ...
- 一名全栈工程师Node.js之路-转
Node.js 全球现状 虽然 Node.js 在国内没有盛行,但据 StackOverflow 2016 年开发者调查,其中 node.js .全栈.JavaScript 相关的技术在多个领域(包括 ...
- linux下开发C语言需要安装的manpages手册
linux下开发C代码需要安装的manpages: $ sudo apt-get install manpages$ sudo apt-get install manpages-dev$ sudo a ...
- Xilinx问题查找
1.登录https://www.xilinx.com/ 2.在All下拉菜单选择Support选项查找技术问题,All选项会查找全部关键词,但是大多数我们只需要技术相关的内容
- modelsim仿真中 do文件的写法技巧
网上的关于DO文件的编写好像资料不多,比较杂,所以本人总结一下常用的简单语法,方便大家查看.其实本人也刚接触DO文件没多久,有纰漏很正常,欢迎指正批评,互相学习.PS:写得有点乱 还有一个值得注意 ...
- 【C/C++语言】int 在计算机内部的存储
int在32位计算机中占4个字节,主要是想弄清楚这4个字节的在内存中存放的顺序. #include <iostream> using namespace std; typedef stru ...
- 如何生成项目的chm文档
如何生成项目的chm文档 2014-11-30 Generate .chm based documentation of your project using SandCastle tool
- echarts 百度图表
手册说明 http://echarts.baidu.com/option.html 3.0版本加了很多新属性可以看以上链接 此文用的3.x版本 第一步 引入 <script src=" ...
- myeclipse 上安装 Maven3<转>
环境准备: JDK 1.6 Maven 3.0.4 myeclipse 8.6.1 安装 Maven 之前要求先确定你的 JDK 已经安装配置完成.Maven是 Apache 下的一个项目,目前最新版 ...
- JavaScript概述.pdf
第1章 JavaScript概述 第2章 使用JavaScript 第3章 语法.关键保留字及变量 第4章 数据类型 第5章 运算符 第6章 流程控制语句 第7章 函数 //没有参数的函数 funct ...