#include <iostream> #include <semaphore> #include <thread> using namespace std; std::counting_semaphore<3> csem(0); // semaphore release = condition_variable notify // semaphore acquire = condition_variable wait void task() { cout << "task:ready to recv signal \n"; csem.acquire(); cout << "task:acquire end\n"; } int main() { thread t0(task); thread t1(task); thread t2(task); thread t3(task); thread t4(task); cout << "main:ready to signal :release\n"; csem.release(3); cout << "main: signal end\n"; t0.join(); t1.join(); t2.join(); t3.join(); t4.join(); }
标签:std,20,C++,信号量,semaphore,include From: https://www.cnblogs.com/FastEarth/p/17868869.html