CppCommon  1.0.4.1
C++ Common Library
wait_ring.h
Go to the documentation of this file.
1 
9 #ifndef CPPCOMMON_THREADS_WAIT_RING_H
10 #define CPPCOMMON_THREADS_WAIT_RING_H
11 
12 #include "condition_variable.h"
13 
14 #include <cassert>
15 #include <vector>
16 
17 namespace CppCommon {
18 
20 
29 template<typename T>
30 class WaitRing
31 {
32 public:
34 
37  explicit WaitRing(size_t capacity);
38  WaitRing(const WaitRing&) = delete;
39  WaitRing(WaitRing&&) = delete;
40  ~WaitRing();
41 
42  WaitRing& operator=(const WaitRing&) = delete;
43  WaitRing& operator=(WaitRing&&) = delete;
44 
46  explicit operator bool() const noexcept { return !closed() && !empty(); }
47 
49  bool closed() const;
50 
52  bool empty() const { return (size() == 0); }
54  size_t capacity() const { return _capacity; }
56  size_t size() const;
57 
59 
67  bool Enqueue(const T& item);
69 
77  bool Enqueue(T&& item);
78 
80 
88  bool Dequeue(T& item);
89 
91 
94  void Close();
95 
96 private:
97  bool _closed;
98  mutable CriticalSection _cs;
99  ConditionVariable _cv1;
100  ConditionVariable _cv2;
101  const size_t _capacity;
102  const size_t _mask;
103  size_t _head;
104  size_t _tail;
105  std::vector<T> _ring;
106 };
107 
110 } // namespace CppCommon
111 
112 #include "wait_ring.inl"
113 
114 #endif // CPPCOMMON_THREADS_WAIT_RING_H
Condition variable synchronization primitive.
Critical section synchronization primitive.
Multiple producers / multiple consumers wait ring.
Definition: wait_ring.h:31
WaitRing(WaitRing &&)=delete
bool closed() const
Is wait ring closed?
Definition: wait_ring.inl:25
size_t capacity() const
Get wait ring capacity.
Definition: wait_ring.h:54
WaitRing(size_t capacity)
Default class constructor.
Definition: wait_ring.inl:12
bool Enqueue(const T &item)
Enqueue an item into the wait ring.
Definition: wait_ring.inl:39
bool Dequeue(T &item)
Dequeue an item from the wait ring.
Definition: wait_ring.inl:70
WaitRing & operator=(const WaitRing &)=delete
bool empty() const
Is wait ring empty?
Definition: wait_ring.h:52
size_t size() const
Get wait ring size.
Definition: wait_ring.inl:32
void Close()
Close the wait ring.
Definition: wait_ring.inl:94
WaitRing & operator=(WaitRing &&)=delete
WaitRing(const WaitRing &)=delete
Condition variable synchronization primitive definition.
C++ Common project definitions.
Definition: token_bucket.h:15
Multiple producers / multiple consumers wait ring inline implementation.