CppCommon 1.0.5.0
C++ Common Library
Loading...
Searching...
No Matches
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
17namespace CppCommon {
18
20
30template<typename T>
32{
33public:
35
39 explicit WaitBatcher(size_t capacity = 0, size_t initial = 0);
40 WaitBatcher(const WaitBatcher&) = delete;
43
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
108private:
109 bool _closed;
110 const size_t _capacity;
111 mutable CriticalSection _cs;
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.
void Close()
Close the wait batcher.
bool Dequeue(std::vector< T > &items)
Dequeue all items from the wait batcher.
WaitBatcher & operator=(const WaitBatcher &)=delete
bool closed() const
Is wait batcher closed?
bool Enqueue(const T &item)
Enqueue an item into the wait batcher.
WaitBatcher(const WaitBatcher &)=delete
WaitBatcher & operator=(WaitBatcher &&)=delete
size_t capacity() const
Get wait batcher capacity.
size_t size() const
Get wait batcher size.
bool empty() const
Is wait batcher empty?
WaitBatcher(WaitBatcher &&)=delete
Condition variable synchronization primitive definition.
C++ Common project definitions.
Multiple producers / multiple consumers wait batcher inline implementation.