Easy
std::latch and std::barrier across a three-phase loop
Three worker threads run worker(id). startLine is a std::latch(3); checkpoint is a std::barrier(3, completion-fn) used across a 3-phase loop. How many times does "phase done" print, and can startLine be reused for a second synchronization event the way checkpoint is reused across phases?
main.cpp
std::latch startLine(3);
std::barrier checkpoint(3, []{ std::cout << "phase done\n"; });
void worker(int id) {
startLine.arrive_and_wait(); // wait for all 3 to be ready
for (int phase = 0; phase < 3; ++phase) {
// ... do this phase's work ...
checkpoint.arrive_and_wait(); // wait for all 3 to finish this phase
}
}23 of 51 in Concurrency