ion 0.12.0
Atomic concurrency support library
Loading...
Searching...
No Matches
ion::FlexBarrier< CompletionFunction > Class Template Reference

Reusable thread-coordination mechanism similar to ion::Barrier but allows control where the completion function is invoked. More...

#include <ion/barrier.hpp>

Public Member Functions

constexpr FlexBarrier (std::uint32_t expected, CompletionFunction func=CompletionFunction()) noexcept
 Construct new barrier with an expected count of expected.
 
template<class WaitFunction = detail::NoOp>
void ArriveAndWait (InvokePolicy policy, std::uint32_t n=1u, WaitFunction &&wait=WaitFunction()) noexcept
 Arrives at barrier, waits until number of arrivals reaches the expected number and depending on policy it may execute the completion function.
 
FlexBarrier requires stable address and is neither copyable or movable.
 FlexBarrier (FlexBarrier const &)=delete
 
FlexBarrieroperator= (FlexBarrier const &)=delete
 

Static Public Member Functions

static constexpr auto Max () noexcept -> std::uint32_t
 

Detailed Description

template<class CompletionFunction = detail::NoOp>
class ion::FlexBarrier< CompletionFunction >

Reusable thread-coordination mechanism similar to ion::Barrier but allows control where the completion function is invoked.

Template Parameters
CompletionFunctionno-except invocable function, invoked by one of the threads that arrive at the barrier via ArriveAndWait() and specified policy InvokePolicy::Allowed.
Memory Order

All calls to ArriveAndWait() synchronizes-with any calls to ArriveAndWait().

The CompletionFunction synchronizes-with calls to ArriveAndWait() after it is executed. This means that no additional synchronization is necessary in the CompletionFunction* w.r.t. to the threads that have arrived and is waiting for departure.

Thread Safety
thread-safe

Constructor & Destructor Documentation

◆ FlexBarrier()

template<class CompletionFunction>
ion::FlexBarrier< CompletionFunction >::FlexBarrier ( std::uint32_t expected,
CompletionFunction func = CompletionFunction() )
explicitconstexprnoexcept

Construct new barrier with an expected count of expected.

Precondition
expected <= Barrier::Max()
Parameters
expectedExpected number of arrivals.
funcCompletion function invoked at most once for every barrier phase. phases arrival and departure.

Member Function Documentation

◆ ArriveAndWait()

template<class CompletionFunction>
template<class WaitFunction>
void ion::FlexBarrier< CompletionFunction >::ArriveAndWait ( InvokePolicy policy,
std::uint32_t n = 1u,
WaitFunction && wait = WaitFunction() )
noexcept

Arrives at barrier, waits until number of arrivals reaches the expected number and depending on policy it may execute the completion function.

Precondition
n must be > 0 and <= the expected count for the current barrier phase.

Decrements the expected count by n and iff policy == InvokePolicy::Allowed then it may also invoke the completion function.

This function executes atomically. The call to this function strongly happens-before the start of the phase completion step for the current phase.

The behavior is undefined if n is less than or equal to 0 or greater than the expected count for the current barrier phase.

Parameters
policyPolicy indicating whether the completion function is allowed to be invoked.
nthe value by which the expected count will be decreased.
waitfunction invoked repeatedly while waiting for barrier to be lifted. The wait function is invoked without any synchronization guaranteees and may or may not be invoked at all if calling thread does not have to wait. If it is desired that all threads leave barrier at the same time, user should take special care to not introduce any latency in WaitFunction such that the thread may lag. Generally this feature should be avoided if possible.
Template Parameters
WaitFunctionno-except invocable function with signature void() noexcept. Default type is a no-op. See wait argument for details.

◆ Max()

template<class CompletionFunction>
auto ion::FlexBarrier< CompletionFunction >::Max ( ) -> std::uint32_t
staticconstexprnoexcept
Returns
Maximum possible value for expected count.