在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
并发 练习代码 #include <thread> #include <vector> #include <mutex> #include <iostream> using namespace std; std::mutex mtx; std::condition_variable cv; char arr[] = { 'a','b','c' }; char message = 'a'; void test(int i) { for(int j = 0 ;j < 10;++j){ std::unique_lock<std::mutex> lk(mtx); cv.wait(lk, [=] {return message == arr[i]; }); std::cout << arr[i]; message = arr[(i + 1) % 3]; lk.unlock(); cv.notify_all(); } } int main() { std::thread t[3]; for (int i = 0; i < 3; ++i) { t[i] = std::thread(test,i); } for (int i = 0; i < 3; ++i) { t[i].join(); } std::cout << std::endl; return 0; }
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论