CppCommon  1.0.4.1
C++ Common Library
Public Member Functions | List of all members
CppCommon::Latch Class Reference

Latch synchronization primitive. More...

#include <latch.h>

Public Member Functions

 Latch (int threads) noexcept
 Default class constructor. More...
 
 Latch (const Latch &)=delete
 
 Latch (Latch &&)=delete
 
 ~Latch ()=default
 
Latchoperator= (const Latch &)=delete
 
Latchoperator= (Latch &&)=delete
 
int threads () const noexcept
 Get the count of threads to wait for the latch. More...
 
void Reset (int threads) noexcept
 Reset the latch with a new threads counter value. More...
 
void CountDown () noexcept
 Countdown the latch. More...
 
void CountDownAndWait () noexcept
 Countdown the latch. More...
 
void Wait () noexcept
 Wait for the latch. More...
 
bool TryWait () noexcept
 Try to wait for the latch without block. More...
 
bool TryWaitFor (const Timespan &timespan) noexcept
 Try to wait for the latch for the given timespan. More...
 
bool TryWaitUntil (const Timestamp &timestamp) noexcept
 Try to wait for the latch until the given timestamp. More...
 

Detailed Description

Latch synchronization primitive.

Latches are a thread co-ordination mechanism that allow one or more threads to block until one or more threads have reached a point.

Thread-safe.

Examples
threads_latch_multi.cpp, and threads_latch_single.cpp.

Definition at line 28 of file latch.h.

Constructor & Destructor Documentation

◆ Latch() [1/3]

CppCommon::Latch::Latch ( int  threads)
inlineexplicitnoexcept

Default class constructor.

Parameters
threads- Latch threads counter initial value

Definition at line 11 of file latch.inl.

◆ Latch() [2/3]

CppCommon::Latch::Latch ( const Latch )
delete

◆ Latch() [3/3]

CppCommon::Latch::Latch ( Latch &&  )
delete

◆ ~Latch()

CppCommon::Latch::~Latch ( )
default

Member Function Documentation

◆ CountDown()

void CppCommon::Latch::CountDown ( )
noexcept

Countdown the latch.

Decrements the latch counter by 1, and returns. If the latch counter reaches 0, any threads blocked in Wait() will be released.

Will not block.

Examples
threads_latch_multi.cpp, and threads_latch_single.cpp.

Definition at line 42 of file latch.cpp.

◆ CountDownAndWait()

void CppCommon::Latch::CountDownAndWait ( )
noexcept

Countdown the latch.

Decrements the latch counter by 1. If the latch counter is not 0, blocks the calling thread until the latch counter is decremented to 0 by one or more other threads calling CountDown() or CountDownAndWait().

Will block.

Definition at line 50 of file latch.cpp.

◆ operator=() [1/2]

Latch& CppCommon::Latch::operator= ( const Latch )
delete

◆ operator=() [2/2]

Latch& CppCommon::Latch::operator= ( Latch &&  )
delete

◆ Reset()

void CppCommon::Latch::Reset ( int  threads)
noexcept

Reset the latch with a new threads counter value.

This method may only be invoked when there are no other threads currently inside the waiting for the latch.

Will not block.

Parameters
threads- Latch threads counter value

Definition at line 13 of file latch.cpp.

◆ threads()

int CppCommon::Latch::threads ( ) const
inlinenoexcept

Get the count of threads to wait for the latch.

Definition at line 44 of file latch.h.

◆ TryWait()

bool CppCommon::Latch::TryWait ( )
noexcept

Try to wait for the latch without block.

Will not block.

Returns
'true' if the latch counter is zero, 'false' if the latch counter is not zero

Definition at line 80 of file latch.cpp.

◆ TryWaitFor()

bool CppCommon::Latch::TryWaitFor ( const Timespan timespan)
inlinenoexcept

Try to wait for the latch for the given timespan.

Will block for the given timespan in the worst case.

Parameters
timespan- Timepan to wait for the latch
Returns
'true' if the latch counter is zero, 'false' if the latch counter is not zero

Definition at line 16 of file latch.inl.

◆ TryWaitUntil()

bool CppCommon::Latch::TryWaitUntil ( const Timestamp timestamp)
inlinenoexcept

Try to wait for the latch until the given timestamp.

Will block until the given timestamp in the worst case.

Parameters
timestamp- Timestamp to stop wait for the latch
Returns
'true' if the latch counter is zero, 'false' if the latch counter is not zero

Definition at line 31 of file latch.inl.

◆ Wait()

void CppCommon::Latch::Wait ( )
noexcept

Wait for the latch.

Method will block the current thread until the latch counter is zero.

Will block.

Examples
threads_latch_multi.cpp, and threads_latch_single.cpp.

Definition at line 65 of file latch.cpp.


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