jaulib v1.3.6
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
jau::latch Class Reference

Inspired by std::latch of C++20. More...

#include <latch.hpp>

Collaboration diagram for jau::latch:

Public Member Functions

 latch () noexcept=default
 Initialize instance with counter zero.
 
 latch (const latch &o)=delete
 No copy constructor nor move constructor.
 
 latch (const size_t count_) noexcept
 Initialize instance with given counter.
 
void arrive_and_wait (const size_t n=1) noexcept
 Atomically decrements the internal counter by n and (if necessary) blocks the calling thread until the counter reaches zero.
 
bool arrive_and_wait_for (const fraction_i64 &timeout_duration, const size_t n=1) noexcept
 Atomically decrements the internal counter by n and (if necessary) blocks the calling thread until the counter reaches zero or the given timeout duration has expired.
 
template<typename Rep, typename Period>
bool arrive_and_wait_for (const std::chrono::duration< Rep, Period > &timeout_duration, const size_t n=1) noexcept
 Atomically decrements the internal counter by n and (if necessary) blocks the calling thread until the counter reaches zero or the given timeout duration has expired.
 
void count_down (const size_t n=1) noexcept
 Atomically decrements the internal counter by n and notifies all blocked wait() threads if zero is reached.
 
void count_up (const size_t n=1) noexcept
 Atomically increments the internal counter by n, i.e.
 
void set (const size_t n) noexcept
 Atomically set the internal counter by n.
 
bool try_wait () const noexcept
 Returns true only if the internal counter has reached zero.
 
size_t value () const noexcept
 Return the current atomic internal counter.
 
void wait () const noexcept
 Blocks the calling thread until the internal counter reaches 0.
 
bool wait_for (const fraction_i64 &timeout_duration) const noexcept
 Blocks the calling thread until the internal counter reaches 0 or the given timeout duration has expired.
 
template<typename Rep, typename Period>
bool wait_for (const std::chrono::duration< Rep, Period > &timeout_duration) const noexcept
 Blocks the calling thread until the internal counter reaches 0 or the given timeout duration has expired.
 

Static Public Member Functions

static constexpr size_t max () noexcept
 Returns the maximum value of the internal counter supported by the implementation.
 

Detailed Description

Inspired by std::latch of C++20.

Adds count_up() as an extension, allowing to dynamically add events to required to complete.

See also
https://en.cppreference.com/w/cpp/thread/latch

Definition at line 50 of file latch.hpp.

Constructor & Destructor Documentation

◆ latch() [1/3]

jau::latch::latch ( )
defaultnoexcept

Initialize instance with counter zero.

Useful for member instances in combination with count_up() or with set() before count_down().

Extension of std::latch.

Here is the caller graph for this function:

◆ latch() [2/3]

jau::latch::latch ( const size_t count_)
inlinenoexcept

Initialize instance with given counter.

Compatible with std::latch.

Parameters
count_

Definition at line 76 of file latch.hpp.

◆ latch() [3/3]

jau::latch::latch ( const latch & o)
delete

No copy constructor nor move constructor.

Compatible with std::latch.

Parameters
o

Member Function Documentation

◆ max()

static constexpr size_t jau::latch::max ( )
inlinestaticconstexprnoexcept

Returns the maximum value of the internal counter supported by the implementation.

Definition at line 58 of file latch.hpp.

◆ value()

size_t jau::latch::value ( ) const
inlinenoexcept

Return the current atomic internal counter.

Extension of std::latch.

Definition at line 93 of file latch.hpp.

Here is the caller graph for this function:

◆ count_down()

void jau::latch::count_down ( const size_t n = 1)
inlinenoexcept

Atomically decrements the internal counter by n and notifies all blocked wait() threads if zero is reached.

If n is greater than the value of the internal counter, the counter is set to zero.

This operation strongly happens-before all calls that are unblocked on this latch.

Compatible with std::latch.

Parameters
nthe value by which the internal counter is decreased, defaults to 1

Definition at line 107 of file latch.hpp.

Here is the caller graph for this function:

◆ count_up()

void jau::latch::count_up ( const size_t n = 1)
inlinenoexcept

Atomically increments the internal counter by n, i.e.

adds value to count down.

If internal counter + n is greater than the maximum value of the internal counter, the counter is set to its maximum.

This operation strongly happens-before all calls that are unblocked on this latch.

Extension of std::latch.

Parameters
nthe value by which the internal counter is increased, defaults to 1

Definition at line 135 of file latch.hpp.

◆ set()

void jau::latch::set ( const size_t n)
inlinenoexcept

Atomically set the internal counter by n.

This operation strongly happens-before all calls that are unblocked on this latch.

Extension of std::latch.

Parameters
nthe value to be assigned to the internal counter

Definition at line 153 of file latch.hpp.

Here is the caller graph for this function:

◆ try_wait()

bool jau::latch::try_wait ( ) const
inlinenoexcept

Returns true only if the internal counter has reached zero.

Compatible with std::latch.

Definition at line 163 of file latch.hpp.

◆ wait()

void jau::latch::wait ( ) const
inlinenoexcept

Blocks the calling thread until the internal counter reaches 0.

If the internal counter is zero already, returns immediately.

Compatible with std::latch.

Definition at line 174 of file latch.hpp.

Here is the caller graph for this function:

◆ arrive_and_wait()

void jau::latch::arrive_and_wait ( const size_t n = 1)
inlinenoexcept

Atomically decrements the internal counter by n and (if necessary) blocks the calling thread until the counter reaches zero.

Equivalent to count_down(n); wait();.

Compatible with std::latch.

Parameters
nthe value by which the internal counter is decreased, defaults to 1

Definition at line 192 of file latch.hpp.

Here is the caller graph for this function:

◆ wait_for() [1/2]

bool jau::latch::wait_for ( const fraction_i64 & timeout_duration) const
inlinenoexcept

Blocks the calling thread until the internal counter reaches 0 or the given timeout duration has expired.

If the internal counter is zero already, returns immediately.

Implementation uses wait_for() w/ a monotonic clock and fraction_i64.

Extension of std::latch.

Parameters
timeout_durationmaximum duration in fractions of seconds to wait
Returns
true if internal counter has reached zero, otherwise a timeout has occurred.

Definition at line 209 of file latch.hpp.

Here is the caller graph for this function:

◆ arrive_and_wait_for() [1/2]

bool jau::latch::arrive_and_wait_for ( const fraction_i64 & timeout_duration,
const size_t n = 1 )
inlinenoexcept

Atomically decrements the internal counter by n and (if necessary) blocks the calling thread until the counter reaches zero or the given timeout duration has expired.

Equivalent to count_down(n); wait(timeout_duration);.

Implementation uses std::chrono::steady_clock::now() and fraction_i64.

Extension of std::latch.

Parameters
timeout_durationmaximum duration in fractions of seconds to wait
nthe value by which the internal counter is decreased, defaults to 1
Returns
true if internal counter has reached zero, otherwise a timeout has occurred.

Definition at line 240 of file latch.hpp.

Here is the caller graph for this function:

◆ wait_for() [2/2]

template<typename Rep, typename Period>
bool jau::latch::wait_for ( const std::chrono::duration< Rep, Period > & timeout_duration) const
inlinenoexcept

Blocks the calling thread until the internal counter reaches 0 or the given timeout duration has expired.

If the internal counter is zero already, returns immediately.

Implementation uses std::chrono::steady_clock::now().

Extension of std::latch.

Template Parameters
Rep
Period
Parameters
timeout_durationmaximum duration to wait
Returns
true if internal counter has reached zero, otherwise a timeout has occurred.

Definition at line 260 of file latch.hpp.

◆ arrive_and_wait_for() [2/2]

template<typename Rep, typename Period>
bool jau::latch::arrive_and_wait_for ( const std::chrono::duration< Rep, Period > & timeout_duration,
const size_t n = 1 )
inlinenoexcept

Atomically decrements the internal counter by n and (if necessary) blocks the calling thread until the counter reaches zero or the given timeout duration has expired.

Equivalent to count_down(n); wait(timeout_duration);.

Implementation uses std::chrono::steady_clock::now().

Extension of std::latch.

Template Parameters
Rep
Period
Parameters
timeout_durationmaximum duration to wait
nthe value by which the internal counter is decreased, defaults to 1
Returns
true if internal counter has reached zero, otherwise a timeout has occurred.

Definition at line 294 of file latch.hpp.


The documentation for this class was generated from the following file: