CppCommon  1.0.4.1
C++ Common Library
wait_batcher.h
Go to the documentation of this file.
1 
9 #ifndef CPPCOMMON_THREADS_WAIT_BATCHER_H
10 #define CPPCOMMON_THREADS_WAIT_BATCHER_H
11 
12 #include "condition_variable.h"
13 
14 #include <cassert>
15 #include <vector>
16 
17 namespace CppCommon {
18 
20 
30 template<typename T>
32 {
33 public:
35 
39  explicit WaitBatcher(size_t capacity = 0, size_t initial = 0);
40  WaitBatcher(const WaitBatcher&) = delete;
41  WaitBatcher(WaitBatcher&&) = delete;
42  ~WaitBatcher();
43 
44  WaitBatcher& operator=(const WaitBatcher&) = delete;
46 
48  explicit operator bool() const noexcept { return !closed() && !empty(); }
49 
51  bool closed() const;
52 
54  bool empty() const { return (size() == 0); }
56  size_t capacity() const;
58  size_t size() const;
59 
61 
69  bool Enqueue(const T& item);
71 
79  bool Enqueue(T&& item);
81 
90  template <class InputIterator>
91  bool Enqueue(InputIterator first, InputIterator last);
92 
94 
100  bool Dequeue(std::vector<T>& items);
101 
103 
106  void Close();
107 
108 private:
109  bool _closed;
110  const size_t _capacity;
111  mutable CriticalSection _cs;
112  ConditionVariable _cv1;
113  ConditionVariable _cv2;
114  std::vector<T> _batch;
115 };
116 
119 } // namespace CppCommon
120 
121 #include "wait_batcher.inl"
122 
123 #endif // CPPCOMMON_THREADS_WAIT_BATCHER_H
Condition variable synchronization primitive.
Critical section synchronization primitive.
Multiple producers / multiple consumers wait batcher.
Definition: wait_batcher.h:32
void Close()
Close the wait batcher.
bool Dequeue(std::vector< T > &items)
Dequeue all items from the wait batcher.
bool closed() const
Is wait batcher closed?
bool Enqueue(const T &item)
Enqueue an item into the wait batcher.
WaitBatcher(const WaitBatcher &)=delete
size_t capacity() const
Get wait batcher capacity.
WaitBatcher(size_t capacity=0, size_t initial=0)
Default class constructor.
size_t size() const
Get wait batcher size.
bool empty() const
Is wait batcher empty?
Definition: wait_batcher.h:54
WaitBatcher & operator=(WaitBatcher &&)=delete
WaitBatcher & operator=(const WaitBatcher &)=delete
WaitBatcher(WaitBatcher &&)=delete
Condition variable synchronization primitive definition.
C++ Common project definitions.
Definition: token_bucket.h:15
Multiple producers / multiple consumers wait batcher inline implementation.