cpp/thread/barrier

The class template provides a thread-coordination mechanism that blocks a group of threads of known size until all threads in that group have reached the barrier. Unlike std, barriers are reusable: once a group of arriving threads are unblocked, the barrier can be reused. Unlike std, barriers execute a possibly empty callable before unblocking threads.

A barrier object's lifetime consists of one or more phases. Each phase defines a phase synchronization point where waiting threads block. Threads can arrive at the barrier, but defer waiting on the phase synchronization point by calling. Such threads can later block on the phase synchronization point by calling.

A barrier phase consists of the following steps:
 * 1) The expected count is decremented by each call to  or.
 * 2) When the expected count reaches zero, the phase completion step is run, meaning that the  is invoked, and all threads blocked on the phase synchronization point are unblocked. The end of the completion step strongly happens-before all calls that were unblocked by the completion step return. Exactly once after the expected count reaches zero, a thread executes the completion step during its call to, , or , except that it is implementation-defined whether the step executes if no thread calls.
 * 3) When the completion step finishes, the expected count is reset to the value specified at construction less the number of calls to  since, and the next barrier phase begins.

Concurrent invocations of the member functions of, except for the destructor, do not introduce data races.

Template parameters
The default template argument of is an unspecified function object type that additionally meets the requirements of. Calling an lvalue of it with no arguments has no effects.

Every barrier object behaves as if it holds an exposition-only non-static data member of type  and calls it by  on every phase completion step.