Thrill  0.1
ThreadBarrierSpin Class Reference

Detailed Description

Implements a thread barrier using atomics and a spin lock that can be used to synchronize threads.

This ThreadBarrier implementation was a lot faster in tests than ThreadBarrierMutex, but ThreadSanitizer shows data races (probably due to the generation counter).

Definition at line 29 of file thread_barrier_spin.hpp.

#include <thread_barrier_spin.hpp>

Public Member Functions

 ThreadBarrierSpin (size_t thread_count)
 Creates a new barrier that waits for n threads. More...
 
size_t step () const
 Return generation step counter. 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, yield thread while spinning. More...
 

Protected Attributes

std::atomic< size_t > step_ { 0 }
 barrier synchronization generation More...
 
const size_t thread_count_
 number of threads, minus one due to comparison needed in loop More...
 
std::atomic< size_t > waiting_ { 0 }
 number of threads in spin lock More...
 

Constructor & Destructor Documentation

◆ ThreadBarrierSpin()

ThreadBarrierSpin ( size_t  thread_count)
inlineexplicit

Creates a new barrier that waits for n threads.

Definition at line 35 of file thread_barrier_spin.hpp.

Member Function Documentation

◆ step()

size_t step ( ) const
inline

Return generation step counter.

Definition at line 97 of file thread_barrier_spin.hpp.

References ThreadBarrierSpin::step_.

Referenced by FlowControlChannel::GetNextStep().

◆ wait()

void wait ( Lambda  lambda = Lambda())
inline

Waits for n threads to arrive.

When they have arrive, execute lambda on the one thread, which arrived last. After lambda, step the generation counter.

This method blocks and returns as soon as n threads are waiting inside the method.

Definition at line 47 of file thread_barrier_spin.hpp.

References ThreadBarrierSpin::step_, ThreadBarrierSpin::thread_count_, and ThreadBarrierSpin::waiting_.

Referenced by FlowControlChannel::AllGather(), FlowControlChannel::AllReduce(), FlowControlChannel::Barrier(), FlowControlChannel::Broadcast(), FlowControlChannel::ExPrefixSumTotal(), FlowControlChannel::LocalBarrier(), FlowControlChannel::Predecessor(), FlowControlChannel::PrefixSumBase(), and FlowControlChannel::Reduce().

◆ wait_yield()

void wait_yield ( Lambda  lambda = Lambda())
inline

Waits for n threads to arrive, yield thread while spinning.

When they have arrive, execute lambda on the one thread, which arrived last. After lambda, step the generation counter.

This method blocks and returns as soon as n threads are waiting inside the method.

Definition at line 76 of file thread_barrier_spin.hpp.

References ThreadBarrierSpin::step_, ThreadBarrierSpin::thread_count_, and ThreadBarrierSpin::waiting_.

Member Data Documentation

◆ step_

std::atomic<size_t> step_ { 0 }
protected

barrier synchronization generation

Definition at line 109 of file thread_barrier_spin.hpp.

Referenced by ThreadBarrierSpin::step(), ThreadBarrierSpin::wait(), and ThreadBarrierSpin::wait_yield().

◆ thread_count_

const size_t thread_count_
protected

number of threads, minus one due to comparison needed in loop

Definition at line 103 of file thread_barrier_spin.hpp.

Referenced by ThreadBarrierSpin::wait(), and ThreadBarrierSpin::wait_yield().

◆ waiting_

std::atomic<size_t> waiting_ { 0 }
protected

number of threads in spin lock

Definition at line 106 of file thread_barrier_spin.hpp.

Referenced by ThreadBarrierSpin::wait(), and ThreadBarrierSpin::wait_yield().


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