25 #ifndef __I6ENGINE_UTILS_DOUBLEBUFFERQUEUE_H__
26 #define __I6ENGINE_UTILS_DOUBLEBUFFERQUEUE_H__
32 #include "boost/thread/mutex.hpp"
44 template<
typename T,
bool producer = true,
bool consumer = true>
58 DoubleBufferQueue() : _queueA(), _queueB(), _queueRead(&_queueA), _queueWrite(&_queueB), _readLock(), _writeLock() {
64 void push(
const T & value) {
65 boost::mutex::scoped_lock scopeLock(_writeLock);
66 _queueWrite->push(value);
73 pop(Bool2Type<consumer>());
80 return front(Bool2Type<consumer>());
87 return poll(Bool2Type<consumer>());
94 return _queueRead->empty() && _queueWrite->empty();
101 return _queueRead->size() + _queueWrite->size();
109 while (!_queueRead->empty()) {
114 while (!_queueWrite->empty()) {
124 std::queue<T> _queueA;
125 std::queue<T> _queueB;
130 std::queue<T> * _queueRead;
131 std::queue<T> * _queueWrite;
133 boost::mutex _readLock;
134 boost::mutex _writeLock;
136 void pop(Bool2Type<true>) {
137 static_assert(consumer,
"Consumer must be true here");
138 boost::mutex::scoped_lock scopeLock(_readLock);
139 if (_queueRead->empty()) {
142 if (_queueRead->empty()) {
150 void pop(Bool2Type<false>) {
151 static_assert(!consumer,
"Consumer must be false here");
152 if (_queueRead->empty()) {
155 if (_queueRead->empty()) {
163 T
front(Bool2Type<true>) {
164 static_assert(consumer,
"Consumer must be true here");
165 boost::mutex::scoped_lock scopeLock(_readLock);
166 if (_queueRead->empty()) {
169 if (_queueRead->empty()) {
173 return _queueRead->front();
176 T
front(Bool2Type<false>) {
177 static_assert(!consumer,
"Consumer must be false here");
178 if (_queueRead->empty()) {
181 if (_queueRead->empty()) {
185 return _queueRead->front();
188 T
poll(Bool2Type<true> b) {
189 static_assert(consumer,
"Consumer must be true here");
190 boost::mutex::scoped_lock scopeLock(_readLock);
191 if (_queueRead->empty()) {
194 if (_queueRead->empty()) {
199 T ret = _queueRead->front();
204 T
poll(Bool2Type<false> b) {
205 static_assert(!consumer,
"Consumer must be false here");
206 if (_queueRead->empty()) {
209 if (_queueRead->empty()) {
214 T ret = _queueRead->front();
223 boost::mutex::scoped_lock scopeLock2(_writeLock);
224 if (_queueRead == &_queueA) {
225 _queueWrite = &_queueA;
226 _queueRead = &_queueB;
228 _queueWrite = &_queueB;
229 _queueRead = &_queueA;
void clear()
removes all elements in the queue
DoubleBufferQueue()
default constructor
void pop()
removes first entry of the queue
#define ISIXE_THROW_API(module, message)
size_t size() const
returns size of the queue
T front()
returns first entry of the queue
T poll()
removes first entry of the queue and returns its value
void push(const T &value)
pushes the given value into the queue
bool empty() const
returns true if the queue is empty, otherwise false