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

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

#include <synchronization.h>

Collaboration diagram for kcenon::thread::barrier< CompletionFunction >:
Collaboration graph

Public Member Functions

 barrier (std::ptrdiff_t count, CompletionFunction completion=[](){})
 Constructs a barrier for the given number of threads.
 
void arrive_and_wait ()
 Arrives at the barrier and blocks until all threads arrive.
 
void arrive_and_drop ()
 Decrements the counter without blocking.
 

Private Attributes

std::mutex mutex_
 
std::condition_variable cv_
 
std::ptrdiff_t threshold_
 
std::ptrdiff_t count_
 
std::size_t generation_
 
CompletionFunction completion_
 

Detailed Description

template<typename CompletionFunction = std::function<void()>>
class kcenon::thread::barrier< CompletionFunction >

C++17-compatible barrier implementation.

A reusable thread coordination mechanism that blocks a set of threads until all threads have arrived at the barrier.

Template Parameters
CompletionFunctionA callable invoked when all threads arrive

Definition at line 101 of file synchronization.h.

Constructor & Destructor Documentation

◆ barrier()

template<typename CompletionFunction = std::function<void()>>
kcenon::thread::barrier< CompletionFunction >::barrier ( std::ptrdiff_t count,
CompletionFunction completion = [](){} )
inlineexplicit

Constructs a barrier for the given number of threads.

Parameters
countThe number of threads to synchronize (must be > 0)
completionOptional function called when all threads arrive

Definition at line 108 of file synchronization.h.

109 {})
110 : threshold_(count)
111 , count_(count)
112 , generation_(0)
113 , completion_(std::move(completion)) {}
CompletionFunction completion_
STL namespace.

Member Function Documentation

◆ arrive_and_drop()

template<typename CompletionFunction = std::function<void()>>
void kcenon::thread::barrier< CompletionFunction >::arrive_and_drop ( )
inline

Decrements the counter without blocking.

Equivalent to arrive_and_drop() in C++20. This thread will not participate in future barrier phases.

Definition at line 152 of file synchronization.h.

152 {
153 std::unique_lock<std::mutex> lock(mutex_);
154 --threshold_;
156 }
void arrive_and_wait()
Arrives at the barrier and blocks until all threads arrive.

References kcenon::thread::barrier< CompletionFunction >::arrive_and_wait(), kcenon::thread::barrier< CompletionFunction >::mutex_, and kcenon::thread::barrier< CompletionFunction >::threshold_.

Here is the call graph for this function:

◆ arrive_and_wait()

template<typename CompletionFunction = std::function<void()>>
void kcenon::thread::barrier< CompletionFunction >::arrive_and_wait ( )
inline

Arrives at the barrier and blocks until all threads arrive.

When the last thread arrives:

  1. The completion function is called
  2. All waiting threads are unblocked
  3. The barrier resets for the next phase

Definition at line 123 of file synchronization.h.

123 {
124 std::unique_lock<std::mutex> lock(mutex_);
125 auto gen = generation_;
126
127 if (--count_ == 0) {
128 // Last thread to arrive
129 generation_++;
131
132 // Call completion function (if provided)
133 if constexpr (!std::is_same_v<CompletionFunction, std::function<void()>>) {
134 completion_();
135 } else if (completion_) {
136 completion_();
137 }
138
139 cv_.notify_all();
140 } else {
141 // Wait for all threads to arrive
142 cv_.wait(lock, [this, gen] { return gen != generation_; });
143 }
144 }
std::condition_variable cv_

References kcenon::thread::barrier< CompletionFunction >::completion_, kcenon::thread::barrier< CompletionFunction >::count_, kcenon::thread::barrier< CompletionFunction >::cv_, kcenon::thread::barrier< CompletionFunction >::generation_, kcenon::thread::barrier< CompletionFunction >::mutex_, and kcenon::thread::barrier< CompletionFunction >::threshold_.

Referenced by kcenon::thread::barrier< CompletionFunction >::arrive_and_drop().

Here is the caller graph for this function:

Member Data Documentation

◆ completion_

template<typename CompletionFunction = std::function<void()>>
CompletionFunction kcenon::thread::barrier< CompletionFunction >::completion_
private

◆ count_

template<typename CompletionFunction = std::function<void()>>
std::ptrdiff_t kcenon::thread::barrier< CompletionFunction >::count_
private

◆ cv_

template<typename CompletionFunction = std::function<void()>>
std::condition_variable kcenon::thread::barrier< CompletionFunction >::cv_
mutableprivate

◆ generation_

template<typename CompletionFunction = std::function<void()>>
std::size_t kcenon::thread::barrier< CompletionFunction >::generation_
private

◆ mutex_

template<typename CompletionFunction = std::function<void()>>
std::mutex kcenon::thread::barrier< CompletionFunction >::mutex_
mutableprivate

◆ threshold_

template<typename CompletionFunction = std::function<void()>>
std::ptrdiff_t kcenon::thread::barrier< CompletionFunction >::threshold_
private

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