11 #ifndef TLX_THREAD_BARRIER_SPIN_HEADER 12 #define TLX_THREAD_BARRIER_SPIN_HEADER 46 template <
typename Lambda = NoOperation<
void> >
47 void wait(Lambda lambda = Lambda()) {
49 size_t this_step =
step_.load(std::memory_order_acquire);
53 waiting_.store(0, std::memory_order_release);
57 step_.fetch_add(1, std::memory_order_acq_rel);
61 while (
step_.load(std::memory_order_acquire) == this_step) {
75 template <
typename Lambda = NoOperation<
void> >
78 size_t this_step =
step_.load(std::memory_order_acquire);
82 waiting_.store(0, std::memory_order_release);
86 step_.fetch_add(1, std::memory_order_acq_rel);
90 while (
step_.load(std::memory_order_acquire) == this_step) {
91 std::this_thread::yield();
98 return step_.load(std::memory_order_acquire);
114 #endif // !TLX_THREAD_BARRIER_SPIN_HEADER std::atomic< size_t > waiting_
number of threads in spin lock
const size_t thread_count_
number of threads, minus one due to comparison needed in loop
std::atomic< size_t > step_
barrier synchronization generation
size_t step() const
Return generation step counter.
void wait(Lambda lambda=Lambda())
Waits for n threads to arrive.
ThreadBarrierSpin(size_t thread_count)
Creates a new barrier that waits for n threads.
void wait_yield(Lambda lambda=Lambda())
Waits for n threads to arrive, yield thread while spinning.
Implements a thread barrier using atomics and a spin lock that can be used to synchronize threads...