CppCommon  1.0.4.1
C++ Common Library
spin_barrier.h
Go to the documentation of this file.
1 
9 #ifndef CPPCOMMON_THREADS_SPIN_BARRIER_H
10 #define CPPCOMMON_THREADS_SPIN_BARRIER_H
11 
12 #include <atomic>
13 #include <cassert>
14 
15 namespace CppCommon {
16 
18 
31 {
32 public:
34 
37  explicit SpinBarrier(int threads) noexcept;
38  SpinBarrier(const SpinBarrier&) = delete;
39  SpinBarrier(SpinBarrier&&) = delete;
40  ~SpinBarrier() = default;
41 
42  SpinBarrier& operator=(const SpinBarrier&) = delete;
44 
46  int threads() const noexcept { return _threads; }
47 
49 
54  bool Wait() noexcept;
55 
56 private:
57  std::atomic<int> _counter;
58  std::atomic<int> _generation;
59  int _threads;
60 };
61 
64 } // namespace CppCommon
65 
66 #include "spin_barrier.inl"
67 
68 #endif // CPPCOMMON_THREADS_SPIN_BARRIER_H
Spin barrier synchronization primitive.
Definition: spin_barrier.h:31
int threads() const noexcept
Get the count of threads to wait at the barrier.
Definition: spin_barrier.h:46
SpinBarrier(const SpinBarrier &)=delete
bool Wait() noexcept
Wait at the barrier until all other threads reach this barrier.
SpinBarrier & operator=(const SpinBarrier &)=delete
SpinBarrier(SpinBarrier &&)=delete
SpinBarrier(int threads) noexcept
Default class constructor.
SpinBarrier & operator=(SpinBarrier &&)=delete
C++ Common project definitions.
Definition: token_bucket.h:15