Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
kcenon::thread::latch Class Reference

C++17-compatible latch implementation. More...

#include <synchronization.h>

Collaboration diagram for kcenon::thread::latch:
Collaboration graph

Public Member Functions

 latch (std::ptrdiff_t count)
 Constructs a latch with the given count.
 
void count_down (std::ptrdiff_t n=1)
 Decrements the counter in a non-blocking manner.
 
bool try_wait () const noexcept
 Tests if the internal counter equals zero.
 
void wait () const
 Blocks until the counter reaches zero.
 
void arrive_and_wait (std::ptrdiff_t n=1)
 Decrements the counter and blocks until it reaches zero.
 

Private Attributes

std::mutex mutex_
 
std::condition_variable cv_
 
std::ptrdiff_t count_
 

Detailed Description

C++17-compatible latch implementation.

A downward counter which can be used to synchronize threads. The value of the counter is initialized on creation. Threads may block on the latch until the counter is decremented to zero.

Definition at line 39 of file synchronization.h.

Constructor & Destructor Documentation

◆ latch()

kcenon::thread::latch::latch ( std::ptrdiff_t count)
inlineexplicit

Constructs a latch with the given count.

Parameters
countThe initial value of the counter (must be >= 0)

Definition at line 45 of file synchronization.h.

46 : count_(count) {}

Member Function Documentation

◆ arrive_and_wait()

void kcenon::thread::latch::arrive_and_wait ( std::ptrdiff_t n = 1)
inline

Decrements the counter and blocks until it reaches zero.

Parameters
nThe value to subtract from the counter (default: 1)

Definition at line 81 of file synchronization.h.

81 {
82 count_down(n);
83 wait();
84 }
void count_down(std::ptrdiff_t n=1)
Decrements the counter in a non-blocking manner.
void wait() const
Blocks until the counter reaches zero.

References count_down(), and wait().

Here is the call graph for this function:

◆ count_down()

void kcenon::thread::latch::count_down ( std::ptrdiff_t n = 1)
inline

Decrements the counter in a non-blocking manner.

Parameters
nThe value to subtract from the counter (default: 1)

Definition at line 52 of file synchronization.h.

52 {
53 std::unique_lock<std::mutex> lock(mutex_);
54 count_ -= n;
55 if (count_ <= 0) {
56 cv_.notify_all();
57 }
58 }
std::condition_variable cv_

References count_, cv_, and mutex_.

Referenced by arrive_and_wait().

Here is the caller graph for this function:

◆ try_wait()

bool kcenon::thread::latch::try_wait ( ) const
inlinenoexcept

Tests if the internal counter equals zero.

Returns
true if the counter has reached zero, false otherwise

Definition at line 64 of file synchronization.h.

64 {
65 std::unique_lock<std::mutex> lock(mutex_);
66 return count_ <= 0;
67 }

References count_, and mutex_.

◆ wait()

void kcenon::thread::latch::wait ( ) const
inline

Blocks until the counter reaches zero.

Definition at line 72 of file synchronization.h.

72 {
73 std::unique_lock<std::mutex> lock(mutex_);
74 cv_.wait(lock, [this] { return count_ <= 0; });
75 }

References count_, cv_, and mutex_.

Referenced by arrive_and_wait().

Here is the caller graph for this function:

Member Data Documentation

◆ count_

std::ptrdiff_t kcenon::thread::latch::count_
private

Definition at line 89 of file synchronization.h.

Referenced by count_down(), try_wait(), and wait().

◆ cv_

std::condition_variable kcenon::thread::latch::cv_
mutableprivate

Definition at line 88 of file synchronization.h.

Referenced by count_down(), and wait().

◆ mutex_

std::mutex kcenon::thread::latch::mutex_
mutableprivate

Definition at line 87 of file synchronization.h.

Referenced by count_down(), try_wait(), and wait().


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