[thread.barrier.class] (original) (raw)

32 Concurrency support library [thread]

32.9 Coordination types [thread.coord]

32.9.3 Barriers [thread.barrier]

32.9.3.3 Class template barrier [thread.barrier.class]

namespace std { template<class CompletionFunction = _see below_> class barrier { public: using arrival_token = see below;static constexpr ptrdiff_t max() noexcept;constexpr explicit barrier(ptrdiff_t expected, CompletionFunction f = CompletionFunction());~barrier(); barrier(const barrier&) = delete; barrier& operator=(const barrier&) = delete; arrival_token arrive(ptrdiff_t update = 1);void wait(arrival_token&& arrival) const;void arrive_and_wait();void arrive_and_drop();private: CompletionFunction completion; };}

Each barrier phase consists of the following steps:

Each phase defines a phase synchronization point.

Threads that arrive at the barrier during the phase can block on the phase synchronization point by calling wait, and will remain blocked until the phase completion step is run.

The phase completion stepthat is executed at the end of each phase has the following effects:

The end of the completion step strongly happens before the returns from all calls that were unblocked by the completion step.

For specializations that do not have the default value of the CompletionFunction template parameter, the behavior is undefined if any of the barrier object's member functions other than wait are called while the completion step is in progress.

Concurrent invocations of the member functions of barrier, other than its destructor, do not introduce data races.

The member functions arrive and arrive_and_dropexecute atomically.

is_nothrow_invocable_v<CompletionFunction&> shall be true.

The default value of the CompletionFunction template parameter is an unspecified type, such that, in addition to satisfying the requirements of CompletionFunction, it meets the Cpp17DefaultConstructiblerequirements (Table 30) andcompletion() has no effects.

static constexpr ptrdiff_t max() noexcept;

Returns: The maximum expected count that the implementation supports.

constexpr explicit barrier(ptrdiff_t expected, CompletionFunction f = CompletionFunction());

Preconditions: expected >= 0 is true andexpected <= max() is true.

Effects: Sets both the initial expected count for each barrier phase and the current expected count for the first phase to expected.

Initializes completion with std​::​move(f).

Starts the first phase.

[Note 1:

If expected is 0 this object can only be destroyed.

— _end note_]

Throws: Any exception thrown by CompletionFunction's move constructor.

arrival_token arrive(ptrdiff_t update = 1);

Preconditions: update > 0 is true, andupdate is less than or equal to the expected count for the current barrier phase.

Effects: Constructs an object of type arrival_tokenthat is associated with the phase synchronization point for the current phase.

Then, decrements the expected count by update.

Synchronization: The call to arrive strongly happens before the start of the phase completion step for the current phase.

Returns: The constructed arrival_token object.

[Note 2:

This call can cause the completion step for the current phase to start.

— _end note_]

void wait(arrival_token&& arrival) const;

Preconditions: arrival is associated with the phase synchronization point for the current phase or the immediately preceding phase of the same barrier object.

Effects: Blocks at the synchronization point associated with std​::​move(arrival)until the phase completion step of the synchronization point's phase is run.

[Note 3:

If arrival is associated with the synchronization point for a previous phase, the call returns immediately.

— _end note_]

Effects: Equivalent to: wait(arrive()).

Preconditions: The expected count for the current barrier phase is greater than zero.

Effects: Decrements the initial expected count for all subsequent phases by one.

Then decrements the expected count for the current phase by one.

Synchronization: The call to arrive_and_drop strongly happens before the start of the phase completion step for the current phase.

[Note 4:

This call can cause the completion step for the current phase to start.

— _end note_]