C++ 20 线程同步

    std::latch latch(10);
    std::vector<std::thread> pool;
    for (auto i = 0; i < 10; ++i) {
        pool.push_back(std::thread([&,i]() {

            // calc
            auto index = "calc " + std::to_string(i) + "\r\n";
            std::cout << index;

            //计数减一
            latch.count_down();
            
            auto dbg = "return " + std::to_string(i) + "\r\n";

            std::cout << dbg;

            }));
    }
    //阻塞等待计数直到为零
    latch.wait();
posted @ 2021-06-02 11:02  學海無涯  阅读(137)  评论(0编辑  收藏  举报