tlx
ThreadBarrierMutex Class Reference

Implements a thread barrier using mutex locking and condition variables that can be used to synchronize threads. More...

#include <thread_barrier_mutex.hpp>

Public Member Functions

 ThreadBarrierMutex (size_t thread_count)
 Creates a new barrier that waits for n threads. More...
 
template<typename Lambda = NoOperation<void>>
void wait (Lambda lambda=Lambda())
 Waits for n threads to arrive. More...
 
template<typename Lambda = NoOperation<void>>
void wait_yield (Lambda lambda=Lambda())
 Waits for n threads to arrive. More...
 
size_t step () const
 return generation step bit: 0 or 1 More...
 

Protected Attributes

const size_t thread_count_
 number of threads More...
 
std::mutex mutex_
 mutex to synchronize access to the counters More...
 
std::condition_variable cv_
 condition variable everyone waits on for the last thread to signal More...
 
size_t counts_ [2]
 two counters: switch between them every run. More...
 
size_t step_
 current counter used. More...
 

Detailed Description

Implements a thread barrier using mutex locking and condition variables that can be used to synchronize threads.

Definition at line 33 of file thread_barrier_mutex.hpp.

Constructor & Destructor Documentation

◆ ThreadBarrierMutex()

ThreadBarrierMutex ( size_t  thread_count)
inlineexplicit

Creates a new barrier that waits for n threads.

Definition at line 47 of file thread_barrier_mutex.hpp.

Member Function Documentation

◆ step()

size_t step ( ) const
inline

return generation step bit: 0 or 1

Definition at line 92 of file thread_barrier_mutex.hpp.

◆ wait()

void wait ( Lambda  lambda = Lambda())
inline

Waits for n threads to arrive.

This method blocks and returns as soon as n threads are waiting inside the method. Prior to continuing work, the lambda functor is called by the last thread entering the barrier.

Definition at line 58 of file thread_barrier_mutex.hpp.

◆ wait_yield()

void wait_yield ( Lambda  lambda = Lambda())
inline

Waits for n threads to arrive.

Identical with wait() for ThreadBarrierMutex.

This method blocks and returns as soon as n threads are waiting inside the method. Prior to continuing work, the lambda functor is called by the last thread entering the barrier.

Definition at line 87 of file thread_barrier_mutex.hpp.

Member Data Documentation

◆ counts_

size_t counts_[2]
protected

two counters: switch between them every run.

Definition at line 107 of file thread_barrier_mutex.hpp.

◆ cv_

std::condition_variable cv_
protected

condition variable everyone waits on for the last thread to signal

Definition at line 104 of file thread_barrier_mutex.hpp.

◆ mutex_

std::mutex mutex_
protected

mutex to synchronize access to the counters

Definition at line 101 of file thread_barrier_mutex.hpp.

◆ step_

size_t step_
protected

current counter used.

Definition at line 110 of file thread_barrier_mutex.hpp.

◆ thread_count_

const size_t thread_count_
protected

number of threads

Definition at line 98 of file thread_barrier_mutex.hpp.


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