C++20(信号量)
#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(); }