std::condition_variable(复习)
#include <iostream> // std::cout
#include <thread> // std::thread
#include <mutex> // std::mutex, std::unique_lock
#include <condition_variable> // std::condition_variable std::mutex mtx; // 全局互斥锁.
std::condition_variable cv; // 全局条件变量.
bool ready = false; // 全局标志位. //1.当一个线程进入时会上锁,然后调用wait会使线程阻塞,然后会解锁
//2.当解锁之后其他线程就可以获得锁,所以所有的线程都会阻塞
//3.notify的时候wait会上锁
//看来这锁和wait操作一起使用可以很好的保证线程的安全,在notif之前能保证只有一个线程访问,在notif之后也能保证只有一个线程访问 void do_print_id(int id)
{
std::unique_lock <std::mutex> lck(mtx);
while (!ready)
cv.wait(lck);
std::cout << "thread " << id << '\n';
} void go()
{
std::unique_lock <std::mutex> lck(mtx);
ready = true; // 设置全局标志位为 true.
cv.notify_all(); // 唤醒所有线程.
} int main()
{
std::thread threads[];
// spawn 10 threads:
for (int i = ; i < ; ++i)
threads[i] = std::thread(do_print_id, i); std::cout << "10 threads ready to race...\n";
go(); // go! for (auto & th:threads)
th.join();
getchar();
return ;
}
std::condition_variable(复习)的更多相关文章
- 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 ...
- 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 ...
- std::condition_variable
/* std::condition_variable 提供了两种 wait() 函数.当前线程调用 wait() 后将被阻塞(此时当前线程应该获得了锁(mutex),不妨设获得锁 lck),直到另外某 ...
- 转 C++11 并发指南std::condition_variable详解
之前看过,但是一直没有怎么用就忘了,转一篇别人的文字记录下来 本文将介绍 C++11 标准中 <condition_variable> 头文件里面的类和相关函数. <conditio ...
- C++11 并发指南五(std::condition_variable 详解)(转)
前面三讲<C++11 并发指南二(std::thread 详解)>,<C++11 并发指南三(std::mutex 详解)>分别介绍了 std::thread,std::mut ...
随机推荐
- java.lang.NoClassDefFoundError: com.doodlemobile.gamecenter.Platform
这时候能够尝试一下下面方法: 右击"项目名"--->"Build path"----->"configure build path&quo ...
- git学习——打标签
打标签 就像某人发布某个软件版本(v1.0)后,一般会给一个标签. Git 使用的标签有两种类型:轻量级的(lightweight)和含附注的(annotated).轻量级标签就像是个不会变化的分支, ...
- 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-如何配置虚拟轴 TC3
在Motion上添加一个NC Task 在Axis上右击添加一个轴,类型为Continuous Axis 在PLC上右击添加新项,然后添加一个PLC项目 在引用中添加TC2_MC2的库引用 ...
- 【POJ 1080】 Human Gene Functions
[POJ 1080] Human Gene Functions 相似于最长公共子序列的做法 dp[i][j]表示 str1[i]相应str2[j]时的最大得分 转移方程为 dp[i][j]=max(d ...
- Oracle 存储过程调用返回游标的另一个存储过程。
一个扩展存储过程调用另一个存储过程,示例: 被调用存储过程:最后会返回一个游标,游标返回一个值.调用这个存储过程的存储过程同样需要获取它. procedure SearchBill --根据到货单号查 ...
- AIDL调用指南
近期有需求要实现两个apk之间的通信,想到用AIDL来实现,现写一个demo学习下AIDL怎样使用. 这里我要实现一个apk(client端)调用还有一个apk(server端)的方法. 先实现ser ...
- 虚拟机实现https网络设置
现在很多网站使用的都是https协议,想在自己的电脑上实现下, 由于自己的电脑是win10,我总是觉得在windows上布置环境不如在linux上稳定,所以在电脑上安装了虚拟机,cento系统 . 上 ...
- C++ Primer Plus的若干收获--(二)
哎,真是不想吐槽考驾照的艰辛历程了.跑到大西郊,顶着大太阳,一天就能摸上个十几分钟二十分钟的车,简直不要太坑爹,这两天真是做的我屁股疼的不行. .. 今天果断不去了.仅仅可惜我的大阿根廷啊,坚持到最后 ...
- kafka快速开始教程
此教程假设你刚刚开始没有任何 Kafka 或 ZooKeeper 数据.Kafka的控制台脚本在类Unix和Windows平台不同,Windows平台使用bin\windows\\代替bin/,脚本的 ...
- Yum Error Another app is currently holding the yum lock; waiting for it to exit
Another app is currently holding the yum lock; waiting for it to exit... The other application is: P ...