ion 0.12.0
Atomic concurrency support library
Loading...
Searching...
No Matches
ion::Latch Class Reference

Single use thread-coordination mechanism modelled after std::latch. More...

#include <ion/latch.hpp>

Public Member Functions

constexpr Latch (std::ptrdiff_t expected) noexcept
 Construct latch with expected number of arrivals.
 
void CountDown (std::ptrdiff_t n=1) noexcept
 Decrements expected count by n without waiting.
 
bool TryWait () const noexcept
 Equivalent to Wait().
 
void Wait () const noexcept
 Block until expected count reaches zero.
 
void ArriveAndWait (std::ptrdiff_t n=1) noexcept
 Decrements expected count by n and waits.
 
Latch requires stable address and is neither copyable or movable.
 Latch (Latch const &)=delete
 
Latchoperator= (Latch const &)=delete
 

Static Public Member Functions

static constexpr std::ptrdiff_t Max () noexcept
 

Detailed Description

Single use thread-coordination mechanism modelled after std::latch.

Modelled after C++20 std::latch but does not perform system calls or yields and as such is more suitable in a real-time environment. Notable differences:

  • Methods are noexcept

Latch has two states:

  1. arrival, when threads fetch and decrement the expected count
  2. departure, when all threads have arrived and will depart.

After departure the only allowed operation is to delete the latch.

Memory Order

All calls to ArriveAndWait() or CountDown() synchronizes-with any calls to ArriveAndWait() or Wait().

Thread Safety
thread-safe

Constructor & Destructor Documentation

◆ Latch()

ion::Latch::Latch ( std::ptrdiff_t expected)
inlineexplicitconstexprnoexcept

Construct latch with expected number of arrivals.

Parameters
expectedExpected number of arrivals.

Member Function Documentation

◆ ArriveAndWait()

void ion::Latch::ArriveAndWait ( std::ptrdiff_t n = 1)
inlinenoexcept

Decrements expected count by n and waits.

Precondition
n must be > 0 and <= the expected count.
Parameters
nthe value by which the internal counter is decreased.

◆ CountDown()

void ion::Latch::CountDown ( std::ptrdiff_t n = 1)
inlinenoexcept

Decrements expected count by n without waiting.

Parameters
nthe value by which the internal counter is decreased.

◆ Max()

std::ptrdiff_t ion::Latch::Max ( )
inlinestaticconstexprnoexcept
Returns
Maximum possible value for expected count.

◆ TryWait()

bool ion::Latch::TryWait ( ) const
inlinenoexcept

Equivalent to Wait().

Note
Method is only provided for conceptual compatibility of std::latch.
Returns
true