42 #ifndef _DWMTHREADQUEUE_HH_ 43 #define _DWMTHREADQUEUE_HH_ 48 #include <condition_variable> 69 template <
typename _ValueType>
77 : _maxLength(0), _queue(), _mutex(), _lock(_mutex), _cv()
87 std::unique_lock<std::mutex> lk(_mutex);
106 _maxLength = maxLength;
113 typename std::deque<_ValueType>::size_type
Length()
const 115 std::lock_guard<std::mutex> lk(_mutex);
116 return _queue.size();
126 std::lock_guard<std::mutex> lk(_mutex);
127 if ((! _maxLength) ||
128 (_queue.size() < _maxLength)) {
129 _queue.push_back(value);
142 template <
typename InputIterator>
143 uint32_t
PushBack(InputIterator firstIter, InputIterator lastIter)
146 if (firstIter != lastIter) {
147 std::lock_guard<std::mutex> lk(_mutex);
148 if ((! _maxLength) ||
149 (_queue.size() < _maxLength)) {
150 uint32_t oldSize = _queue.size();
151 _queue.insert(_queue.end(), firstIter, lastIter);
152 rc = _queue.size() - oldSize;
168 std::lock_guard<std::mutex> lk(_mutex);
169 if ((! _maxLength) ||
170 (_queue.size() < _maxLength)) {
171 _queue.push_front(value);
193 rc = _cv.wait(_lock);
203 template<
class Rep,
class Period>
208 if (_cv.wait_for(_lock, timeToWait) == std::cv_status::no_timeout) {
222 std::lock_guard<std::mutex> lk(_mutex);
223 if (! _queue.empty()) {
224 value = _queue.front();
238 std::lock_guard<std::mutex> lk(_mutex);
239 if (! _queue.empty()) {
240 value = _queue.back();
255 while (_queue.empty()) {
267 template <
class Rep,
class Period>
271 if (_queue.empty()) {
273 if (! _queue.empty()) {
289 return(_queue.empty());
297 std::lock_guard<std::mutex> lk(_mutex);
298 random_shuffle(_queue.begin(), _queue.end());
308 uint32_t
Copy(std::deque<_ValueType> & c)
314 std::lock_guard<std::mutex> lk(_mutex);
315 typename std::deque<_ValueType>::iterator iter = _queue.begin();
316 for ( ; iter != _queue.end(); ++iter) {
331 uint32_t
Swap(std::deque<_ValueType> & c)
333 std::lock_guard<std::mutex> lk(_mutex);
334 if (! _queue.empty()) {
343 std::deque<_ValueType> _queue;
344 mutable std::mutex _mutex;
345 std::unique_lock<std::mutex> _lock;
346 std::condition_variable _cv;
373 #endif // _DWMTHREADQUEUE_HH_ bool PushBack(const _ValueType &value)
Inserts value on the back of the queue.
Definition: DwmThreadQueue.hh:123
void ConditionSignal()
Unblocks at least one thread waiting on the condition variable.
Definition: DwmThreadQueue.hh:181
uint32_t Swap(std::deque< _ValueType > &c)
This member is a simple optimization for fetching the contents of the queue.
Definition: DwmThreadQueue.hh:331
uint32_t Copy(std::deque< _ValueType > &c)
Copies the contents of the queue to c.
Definition: DwmThreadQueue.hh:308
bool ConditionWait()
Waits for the condition variable to be signalled or broadcasted.
Definition: DwmThreadQueue.hh:189
Dwm::SysLogger class definition and Syslog() macro.
bool WaitForNotEmpty()
Blocks the calling thread until the queue contains at least one entry.
Definition: DwmThreadQueue.hh:251
bool PushFront(const _ValueType &value)
Inserts value on the front of the queue.
Definition: DwmThreadQueue.hh:165
Queue()
Constructor.
Definition: DwmThreadQueue.hh:76
uint32_t PushBack(InputIterator firstIter, InputIterator lastIter)
Inserts the values from firstIter to lastIter on the back of the queue.
Definition: DwmThreadQueue.hh:143
uint32_t MaxLength() const
Returns the max length of the queue.
Definition: DwmThreadQueue.hh:95
Definition: DwmBZ2IO.hh:67
~Queue()
Destructor.
Definition: DwmThreadQueue.hh:85
This template provides inter-thread first-in first-out (FIFO) queueing.
Definition: DwmThreadQueue.hh:70
bool PopBack(_ValueType &value)
Pops the entry from the back of the queue and stores it in value.
Definition: DwmThreadQueue.hh:235
uint32_t MaxLength(uint32_t maxLength)
Sets and returns the max length of the queue.
Definition: DwmThreadQueue.hh:104
std::deque< _ValueType >::size_type Length() const
Returns the current length of the queue.
Definition: DwmThreadQueue.hh:113
bool PopFront(_ValueType &value)
Pops the entry from the front of the queue and stores it in value.
Definition: DwmThreadQueue.hh:219
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:204
bool Empty()
Returns true if the queue is empty, else returns false.
Definition: DwmThreadQueue.hh:287
bool TimedWaitForNotEmpty(const std::chrono::duration< Rep, Period > &timeToWait)
Waits timeToWait for the queue to be non-empty.
Definition: DwmThreadQueue.hh:268