22 #ifndef __M2ETIS_UTIL_DOUBLEBUFFERQUEUE_H__
23 #define __M2ETIS_UTIL_DOUBLEBUFFERQUEUE_H__
29 #include "boost/thread/recursive_mutex.hpp"
34 template<
typename T,
bool producer = true,
bool consumer = true>
40 DoubleBufferQueue() : _queueA(), _queueB(), _queueRead(&_queueA), _queueWrite(&_queueB), _lock() {
46 void push(
const T & value) {
47 boost::recursive_mutex::scoped_lock scopeLock(_lock);
48 _queueWrite->push(value);
56 boost::recursive_mutex::scoped_lock scopeLock(_lock);
57 if (_queueRead->empty()) {
61 if (_queueRead->empty()) {
72 boost::recursive_mutex::scoped_lock scopeLock(_lock);
73 if (_queueRead->empty()) {
77 if (_queueRead->empty()) {
81 return _queueRead->front();
88 boost::recursive_mutex::scoped_lock scopeLock(_lock);
89 if (_queueRead->empty()) {
93 if (_queueRead->empty()) {
97 T ret = _queueRead->front();
106 return _queueRead->empty() && _queueWrite->empty();
113 return _queueRead->size() + _queueWrite->size();
120 boost::recursive_mutex::scoped_lock scopeLock(_lock);
121 while(!_queueRead->empty()) {
124 while(!_queueWrite->empty()) {
130 std::queue<T> _queueA;
131 std::queue<T> _queueB;
133 std::queue<T> * _queueRead;
134 std::queue<T> * _queueWrite;
136 boost::recursive_mutex _lock;
144 boost::recursive_mutex::scoped_lock scopeLock(_lock);
145 if (_queueRead == &_queueA) {
146 _queueWrite = &_queueA;
147 _queueRead = &_queueB;
149 _queueWrite = &_queueB;
150 _queueRead = &_queueA;
bool empty() const
returns true if the queue is empty, otherwise false
T front()
returns first entry of the queue
void push(const T &value)
pushes the given value into the queue
DoubleBufferQueue()
default constructor
void clear()
removes all elements in the queue
T poll()
remoes first entry of the queue and returns its value
size_t size() const
returns size of the queue
#define M2ETIS_THROW_API(module, message)
throws on wrong API usage
void pop()
removes first entry of the queue