CppCommon  1.0.4.1
C++ Common Library
wait_queue.h
Go to the documentation of this file.
1 
9 #ifndef CPPCOMMON_THREADS_WAIT_QUEUE_H
10 #define CPPCOMMON_THREADS_WAIT_QUEUE_H
11 
12 #include "condition_variable.h"
13 
14 #include <queue>
15 
16 namespace CppCommon {
17 
19 
28 template<typename T>
29 class WaitQueue
30 {
31 public:
33 
36  explicit WaitQueue(size_t capacity = 0);
37  WaitQueue(const WaitQueue&) = delete;
38  WaitQueue(WaitQueue&&) = default;
39  ~WaitQueue();
40 
41  WaitQueue& operator=(const WaitQueue&) = delete;
42  WaitQueue& operator=(WaitQueue&&) = default;
43 
45  explicit operator bool() const noexcept { return !closed() && !empty(); }
46 
48  bool closed() const;
49 
51  bool empty() const { return (size() == 0); }
53  size_t capacity() const;
55  size_t size() const;
56 
58 
66  bool Enqueue(const T& item);
68 
76  bool Enqueue(T&& item);
77 
79 
87  bool Dequeue(T& item);
88 
90 
93  void Close();
94 
95 private:
96  bool _closed;
97  const size_t _capacity;
98  mutable CriticalSection _cs;
99  ConditionVariable _cv1;
100  ConditionVariable _cv2;
101  std::queue<T> _queue;
102 };
103 
106 } // namespace CppCommon
107 
108 #include "wait_queue.inl"
109 
110 #endif // CPPCOMMON_THREADS_WAIT_QUEUE_H
Condition variable synchronization primitive.
Critical section synchronization primitive.
Multiple producers / multiple consumers wait queue.
Definition: wait_queue.h:30
bool Enqueue(const T &item)
Enqueue an item into the wait queue.
Definition: wait_queue.inl:47
bool empty() const
Is wait queue empty?
Definition: wait_queue.h:51
bool Dequeue(T &item)
Dequeue an item from the wait queue.
Definition: wait_queue.inl:95
void Close()
Close the wait queue.
Definition: wait_queue.inl:120
WaitQueue(size_t capacity=0)
Default class constructor.
Definition: wait_queue.inl:12
size_t capacity() const
Get wait queue capacity.
Definition: wait_queue.inl:30
WaitQueue(const WaitQueue &)=delete
WaitQueue & operator=(WaitQueue &&)=default
WaitQueue(WaitQueue &&)=default
bool closed() const
Is wait queue closed?
Definition: wait_queue.inl:23
WaitQueue & operator=(const WaitQueue &)=delete
size_t size() const
Get wait queue size.
Definition: wait_queue.inl:40
Condition variable synchronization primitive definition.
C++ Common project definitions.
Definition: token_bucket.h:15
Multiple producers / multiple consumers wait queue inline implementation.