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();
转载请注明出处并保持作品的完整性,谢谢