41#ifndef _DWMTHREADQUEUE_HH_
42#define _DWMTHREADQUEUE_HH_
48#include <condition_variable>
66 template <
typename _ValueType>
74 : _maxLength(0), _queue(), _mutex(), _signalled(false),
85 std::unique_lock<std::mutex> lk(_mutex);
104 _maxLength = maxLength;
111 typename std::deque<_ValueType>::size_type
Length()
const
113 std::lock_guard<std::mutex> lk(_mutex);
114 return _queue.size();
124 std::lock_guard<std::mutex> lk(_mutex);
125 if ((! _maxLength) ||
126 (_queue.size() < _maxLength)) {
127 _queue.push_back(value);
142 std::lock_guard<std::mutex> lk(_mutex);
143 if ((! _maxLength) ||
144 (_queue.size() < _maxLength)) {
145 _queue.push_back(value);
159 template <
typename InputIterator>
160 uint32_t
PushBack(InputIterator firstIter, InputIterator lastIter)
163 if (firstIter != lastIter) {
164 std::lock_guard<std::mutex> lk(_mutex);
165 if ((! _maxLength) ||
166 (_queue.size() < _maxLength)) {
167 uint32_t oldSize = _queue.size();
168 _queue.insert(_queue.end(), firstIter, lastIter);
169 rc = _queue.size() - oldSize;
186 std::lock_guard<std::mutex> lk(_mutex);
187 if ((! _maxLength) ||
188 (_queue.size() < _maxLength)) {
189 _queue.push_front(value);
200 template <
typename InputIterator>
201 uint32_t
PushFront(InputIterator firstIter, InputIterator lastIter)
204 if (firstIter != lastIter) {
205 std::lock_guard<std::mutex> lk(_mutex);
206 if ((! _maxLength) ||
207 (_queue.size() < _maxLength)) {
208 uint32_t oldSize = _queue.size();
209 _queue.insert(_queue.begin(), firstIter, lastIter);
210 rc = _queue.size() - oldSize;
235 _cv.wait(_lock, [&] {
return _signalled.load(); });
246 template<
class Rep,
class Period>
251 if (_cv.wait_for(_lock, timeToWait,
252 [&] { return _signalled.load(); })) {
267 std::lock_guard<std::mutex> lk(_mutex);
268 if (! _queue.empty()) {
269 if (std::is_move_assignable<_ValueType>::value) {
270 value = std::move(_queue.front());
273 value = _queue.front();
288 std::lock_guard<std::mutex> lk(_mutex);
289 if (! _queue.empty()) {
290 if (std::is_move_assignable<_ValueType>::value) {
291 value = std::move(_queue.back());
294 value = _queue.back();
310 _cv.wait(_lock, [&] {
return (! _queue.empty()); });
320 template <
class Rep,
class Period>
325 if (! _queue.empty()) {
329 if (_cv.wait_for(_lock, timeToWait,
330 [&] { return _signalled.load(); })) {
331 rc = (! _queue.empty());
344 return(_queue.empty());
352 std::lock_guard<std::mutex> lk(_mutex);
353 random_shuffle(_queue.begin(), _queue.end());
363 uint32_t
Copy(std::deque<_ValueType> & c)
369 std::lock_guard<std::mutex> lk(_mutex);
370 typename std::deque<_ValueType>::iterator iter = _queue.begin();
371 for ( ; iter != _queue.end(); ++iter) {
386 uint32_t
Swap(std::deque<_ValueType> & c)
388 std::lock_guard<std::mutex> lk(_mutex);
389 if (! _queue.empty()) {
398 std::deque<_ValueType> _queue;
399 mutable std::mutex _mutex;
400 std::atomic<bool> _signalled;
401 std::unique_lock<std::mutex> _lock;
402 std::condition_variable _cv;
This template provides inter-thread first-in first-out (FIFO) queueing.
Definition DwmThreadQueue.hh:68
uint32_t PushBack(InputIterator firstIter, InputIterator lastIter)
Inserts the values from firstIter to lastIter on the back of the queue.
Definition DwmThreadQueue.hh:160
std::deque< _ValueType >::size_type Length() const
Returns the current length of the queue.
Definition DwmThreadQueue.hh:111
uint32_t Swap(std::deque< _ValueType > &c)
This member is a simple optimization for fetching the contents of the queue.
Definition DwmThreadQueue.hh:386
bool Empty()
Returns true if the queue is empty, else returns false.
Definition DwmThreadQueue.hh:342
uint32_t Copy(std::deque< _ValueType > &c)
Copies the contents of the queue to c.
Definition DwmThreadQueue.hh:363
uint32_t MaxLength(uint32_t maxLength)
Sets and returns the max length of the queue.
Definition DwmThreadQueue.hh:102
bool ConditionTimedWait(const std::chrono::duration< Rep, Period > &timeToWait)
Waits for the condition variable to be signalled or broadcasted for timeToWait to pass.
Definition DwmThreadQueue.hh:247
bool PushBack(_ValueType &&value)
Inserts value on the back of the queue.
Definition DwmThreadQueue.hh:139
bool PopBack(_ValueType &value)
Pops the entry from the back of the queue and stores it in value.
Definition DwmThreadQueue.hh:285
bool WaitForNotEmpty()
Blocks the calling thread until the queue contains at least one entry.
Definition DwmThreadQueue.hh:306
bool ConditionWait()
Waits for the condition variable to be signalled or broadcasted.
Definition DwmThreadQueue.hh:232
uint32_t MaxLength() const
Returns the max length of the queue.
Definition DwmThreadQueue.hh:93
~Queue()
Destructor.
Definition DwmThreadQueue.hh:83
bool PushBack(const _ValueType &value)
Inserts value on the back of the queue.
Definition DwmThreadQueue.hh:121
bool PopFront(_ValueType &value)
Pops the entry from the front of the queue and stores it in value.
Definition DwmThreadQueue.hh:264
bool PushFront(const _ValueType &value)
Inserts value on the front of the queue.
Definition DwmThreadQueue.hh:183
void ConditionSignal()
Unblocks at least one thread waiting on the condition variable.
Definition DwmThreadQueue.hh:223
Queue()
Constructor.
Definition DwmThreadQueue.hh:73
bool TimedWaitForNotEmpty(const std::chrono::duration< Rep, Period > &timeToWait)
Waits timeToWait for the queue to be non-empty.
Definition DwmThreadQueue.hh:321