CppCommon 1.0.5.0
C++ Common Library
Loading...
Searching...
No Matches
spin_barrier.inl
Go to the documentation of this file.
1
9namespace CppCommon {
10
11inline SpinBarrier::SpinBarrier(int threads) noexcept : _counter(threads), _generation(0), _threads(threads)
12{
13 assert((threads > 0) && "Count of barrier threads must be greater than zero!");
14}
15
16inline bool SpinBarrier::Wait() noexcept
17{
18 // Remember the current barrier generation
19 int generation = _generation;
20
21 // Decrease the count of waiting threads
22 if (--_counter == 0)
23 {
24 // Increase the current barrier generation
25 ++_generation;
26
27 // Reset waiting threads counter
28 _counter = _threads;
29
30 // Notify the last thread that reached the barrier
31 return true;
32 }
33 else
34 {
35 // Spin-wait for the next barrier generation
36 while ((generation == _generation) || (_counter == 0));
37
38 // Notify each of remaining threads
39 return false;
40 }
41}
42
43} // namespace CppCommon
bool Wait() noexcept
Wait at the barrier until all other threads reach this barrier.
SpinBarrier(int threads) noexcept
Default class constructor.
C++ Common project definitions.