25 #ifndef __CLOCKUTILS_CONTAINER_DOUBLEBUFFERQUEUE_H__ 26 #define __CLOCKUTILS_CONTAINER_DOUBLEBUFFERQUEUE_H__ 45 template<
typename T,
bool producer = true,
bool consumer = true>
59 DoubleBufferQueue() : _queueA(), _queueB(), _queueRead(&_queueA), _queueWrite(&_queueB), _readLock(), _writeLock() {
65 void push(
const T & value) {
66 std::lock_guard<std::mutex> lg(_writeLock);
67 _queueWrite->push(value);
74 return pop(Bool2Type<consumer>());
81 return front(Bool2Type<consumer>(), value);
88 return poll(Bool2Type<consumer>(), value);
95 return _queueRead->empty() && _queueWrite->empty();
102 return _queueRead->size() + _queueWrite->size();
110 while (!_queueRead->empty()) {
115 while (!_queueWrite->empty()) {
125 std::queue<T> _queueA;
126 std::queue<T> _queueB;
131 std::queue<T> * _queueRead;
132 std::queue<T> * _queueWrite;
134 std::mutex _readLock;
135 std::mutex _writeLock;
138 static_assert(consumer,
"Consumer must be true here");
139 std::lock_guard<std::mutex> lg(_readLock);
140 if (_queueRead->empty()) {
144 if (_queueRead->empty()) {
153 static_assert(!consumer,
"Consumer must be false here");
154 if (_queueRead->empty()) {
158 if (_queueRead->empty()) {
167 static_assert(consumer,
"Consumer must be true here");
168 std::lock_guard<std::mutex> lg(_readLock);
169 if (_queueRead->empty()) {
173 if (_queueRead->empty()) {
176 value = _queueRead->front();
182 static_assert(!consumer,
"Consumer must be false here");
183 if (_queueRead->empty()) {
187 if (_queueRead->empty()) {
190 value = _queueRead->front();
196 static_assert(consumer,
"Consumer must be true here");
197 std::lock_guard<std::mutex> lg(_readLock);
198 if (_queueRead->empty()) {
202 if (_queueRead->empty()) {
205 value = _queueRead->front();
212 static_assert(!consumer,
"Consumer must be false here");
213 if (_queueRead->empty()) {
217 if (_queueRead->empty()) {
220 value = _queueRead->front();
230 std::lock_guard<std::mutex> lg(_writeLock);
231 if (_queueRead == &_queueA) {
232 _queueWrite = &_queueA;
233 _queueRead = &_queueB;
235 _queueWrite = &_queueB;
236 _queueRead = &_queueA;
void push(const T &value)
pushes the given value into the queue
size_t size() const
returns size of the queue
void clear()
removes all elements in the queue
0x10 no element available
0x0 method call succeeded
ClockError pop()
removes first entry of the queue
DoubleBufferQueue()
default constructor
ClockError poll(T &value)
removes first entry of the queue and returns its value
ClockError front(T &value)
returns first entry of the queue, but keeps it in the queue
bool empty() const
returns true if the queue is empty, otherwise false