条件变量思路整理
来源:互联网 发布:vs画图软件 编辑:程序博客网 时间:2024/05/02 01:13
#include <iostream>
#include <unistd.h>#include <thread>
#include <mutex>
#include <condition_variable>
using namespace std;
int main()
{
std::mutex mtx;
std::condition_variable cv;
int Cnt = -2;
std::thread threads[10];
for (int i = 0; i < 10; ++i)
threads[i] = std::thread([i,&Cnt,&mtx,&cv]() {
std::unique_lock <std::mutex> lck(mtx);
cv.wait(lck, [&i,&Cnt]() {return Cnt == i - 1;});
cout << i << " is weakuped!" << endl;
Cnt++;
cv.notify_all();
});
std::unique_lock <std::mutex> lck(mtx,std::defer_lock);
lck.lock();
Cnt = -1;
cv.notify_all();
lck.unlock();
for (auto & th:threads)
th.join();
return 0;
}
0 0
- 条件变量思路整理
- 整理思路
- 思路整理
- 整理思路
- 思路整理
- 整理整理思路
- 条件变量
- 条件变量
- 条件变量
- 条件变量
- 条件变量
- 条件变量
- 条件变量
- 条件变量
- 条件变量
- 条件变量
- 条件变量
- 条件变量
- 关于EnumerateObjectsUsingBlock和for-in之间的较量
- 排序算法——选择排序
- Hdu 4009 Transfer water【最小树形图】
- 第十一讲 项目3 买五赠一 买二十赠五 优惠计算
- Feature transformations with ensembles of trees
- 条件变量思路整理
- basic_echo_server
- WebView
- c++ string类函数总结
- next_permutation原理剖析
- C++第五次实验-项目2:数组选择
- poll_echo_server
- 我大一的线性代数学习
- 抽象代数——代数结构