- 1. Leetcode并发题
- 1.1 按序打印
- 1.2 交替打印FooBar
博客园 - C++11 并发指南系列
std::condition_variable
1.1 按序打印1114. 按序打印
#include1.2 交替打印FooBar#include using namespace std; class Foo { protected: sem_t first_done; sem_t second_done; public: Foo() { sem_init(&first_done,0,0); sem_init(&second_done,0,0); } void first(function printFirst) { // printFirst() outputs "first". Do not change or remove this line. printFirst(); sem_post(&first_done); } void second(function printSecond) { // printSecond() outputs "second". Do not change or remove this line. sem_wait(&first_done); printSecond(); sem_post(&second_done); } void third(function printThird) { sem_wait(&second_done); // printThird() outputs "third". Do not change or remove this line. printThird(); } };
1115. 交替打印FooBar
【C++】【多方法】
还剩【异步 *** 作】、【原子 *** 作】没学完
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)