Reusable cyclic barrier
Implement a barrier for a fixed number of threads. Each thread calls wait() at a sync point and blocks until all of them have arrived; then all are released together and the barrier resets itself for the next round. Build it from a mutex and condition variables. Handle spurious wakeups, and make sure a fast thread that loops around and calls wait() again cannot slip into the previous round (a generation counter is the usual fix).
No answer yet. Write the first one; the best answer gets endorsed.
