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();
}

 

posted @ 2023-12-01 09:03  经纬视界  阅读(38)  评论(0编辑  收藏  举报