Lunchbox
1.16.0
Multi-threaded C++ toolbox library for all application developers creating high-performance multi-threaded programs.
|
A thread-safe queue with a blocking read access. More...
#include <mtQueue.h>
Classes | |
class | Group |
Group descriptor for popBarrier(). More... | |
Public Types | |
typedef T | value_type |
Public Member Functions | |
MTQueue (const size_t maxSize=S) | |
Construct a new queue. More... | |
MTQueue (const MTQueue< T, S > &from) | |
Construct a copy of a queue. More... | |
~MTQueue () | |
Destruct this Queue. More... | |
MTQueue< T, S > & | operator= (const MTQueue< T, S > &from) |
Assign the values of another queue. More... | |
const T & | operator[] (const size_t index) const |
Retrieve the requested element from the queue, may block. More... | |
bool | isEmpty () const |
size_t | getSize () const |
void | setMaxSize (const size_t maxSize) |
Set the new maximum size of the queue. More... | |
size_t | getMaxSize () const |
size_t | waitSize (const size_t minSize) const |
Wait for the size to be at least the number of given elements. More... | |
void | clear () |
Reset (empty) the queue. More... | |
T | pop () |
Retrieve and pop the front element from the queue, may block. More... | |
bool | timedPop (const unsigned timeout, T &element) |
Retrieve and pop the front element from the queue. More... | |
std::vector< T > | timedPopRange (const unsigned timeout, const size_t minimum=1, const size_t maximum=S) |
Retrieve a number of items from the front of the queue. More... | |
bool | tryPop (T &result) |
Retrieve and pop the front element from the queue if it is not empty. More... | |
void | tryPop (const size_t num, std::vector< T > &result) |
Try to retrieve a number of items from the front of the queue. More... | |
bool | popBarrier (T &result, Group &barrier) |
Retrieve the front element, or abort if the barrier is reached. More... | |
bool | getFront (T &result) const |
bool | getBack (T &result) const |
void | push (const T &element) |
Push a new element to the back of the queue. More... | |
void | push (const std::vector< T > &elements) |
Push a vector of elements to the back of the queue. More... | |
void | pushFront (const T &element) |
Push a new element to the front of the queue. More... | |
void | pushFront (const std::vector< T > &elements) |
Push a vector of elements to the front of the queue. More... | |
STL compatibility. @version 1.7.1 | |
void | push_back (const T &element) |
bool | empty () const |
A thread-safe queue with a blocking read access.
Typically used to communicate between two execution threads.
S is deprecated by the ctor param maxSize, and defines the initial maximum capacity of the Queue<T>. When the capacity is reached, pushing new values blocks until items have been consumed.
Example:
|
inlineexplicit |
Construct a new queue.
Definition at line 54 of file mtQueue.h.
Referenced by lunchbox::MTQueue< T, S >::getMaxSize().
|
inline |
|
inline |
Destruct this Queue.
Definition at line 62 of file mtQueue.h.
References lunchbox::MTQueue< T, S >::operator=(), and lunchbox::MTQueue< T, S >::operator[]().
void lunchbox::MTQueue< T, S >::clear | ( | ) |
Reset (empty) the queue.
Definition at line 68 of file mtQueue.ipp.
Referenced by lunchbox::MTQueue< T, S >::getMaxSize().
bool lunchbox::MTQueue< T, S >::getBack | ( | T & | result | ) | const |
result | the last value or unmodified. |
Definition at line 216 of file mtQueue.ipp.
Referenced by lunchbox::MTQueue< T, S >::getMaxSize().
bool lunchbox::MTQueue< T, S >::getFront | ( | T & | result | ) | const |
result | the front value or unmodified. |
Definition at line 204 of file mtQueue.ipp.
Referenced by lunchbox::MTQueue< T, S >::getMaxSize().
|
inline |
Definition at line 87 of file mtQueue.h.
References lunchbox::MTQueue< T, S >::clear(), lunchbox::MTQueue< T, S >::getBack(), lunchbox::MTQueue< T, S >::getFront(), lunchbox::MTQueue< T, S >::isEmpty(), lunchbox::MTQueue< T, S >::MTQueue(), lunchbox::MTQueue< T, S >::operator=(), lunchbox::MTQueue< T, S >::pop(), lunchbox::MTQueue< T, S >::popBarrier(), lunchbox::MTQueue< T, S >::push(), lunchbox::MTQueue< T, S >::pushFront(), lunchbox::MTQueue< T, S >::timedPop(), lunchbox::MTQueue< T, S >::timedPopRange(), lunchbox::MTQueue< T, S >::tryPop(), and lunchbox::MTQueue< T, S >::waitSize().
|
inline |
Definition at line 75 of file mtQueue.h.
References lunchbox::MTQueue< T, S >::setMaxSize().
|
inline |
Definition at line 73 of file mtQueue.h.
Referenced by lunchbox::MTQueue< T, S >::getMaxSize().
MTQueue< T, S > & lunchbox::MTQueue< T, S >::operator= | ( | const MTQueue< T, S > & | from | ) |
Assign the values of another queue.
Definition at line 22 of file mtQueue.ipp.
Referenced by lunchbox::MTQueue< T, S >::getMaxSize(), and lunchbox::MTQueue< T, S >::~MTQueue().
const T & lunchbox::MTQueue< T, S >::operator[] | ( | const size_t | index | ) | const |
Retrieve the requested element from the queue, may block.
Definition at line 40 of file mtQueue.ipp.
Referenced by lunchbox::MTQueue< T, S >::~MTQueue().
T lunchbox::MTQueue< T, S >::pop | ( | ) |
Retrieve and pop the front element from the queue, may block.
Definition at line 76 of file mtQueue.ipp.
Referenced by lunchbox::MTQueue< T, S >::getMaxSize().
bool lunchbox::MTQueue< T, S >::popBarrier | ( | T & | result, |
Group & | barrier | ||
) |
Retrieve the front element, or abort if the barrier is reached.
Used for worker threads recursively processing data, pushing it back the queue. Either returns an item from the queue, or aborts if num participants are waiting in the queue.
result | the result element, unmodified on false return value. |
barrier | the group's barrier handle. |
Definition at line 179 of file mtQueue.ipp.
Referenced by lunchbox::MTQueue< T, S >::getMaxSize().
void lunchbox::MTQueue< T, S >::push | ( | const T & | element | ) |
Push a new element to the back of the queue.
Definition at line 228 of file mtQueue.ipp.
Referenced by lunchbox::MTQueue< T, S >::getMaxSize().
void lunchbox::MTQueue< T, S >::push | ( | const std::vector< T > & | elements | ) |
Push a vector of elements to the back of the queue.
Definition at line 237 of file mtQueue.ipp.
void lunchbox::MTQueue< T, S >::pushFront | ( | const T & | element | ) |
Push a new element to the front of the queue.
Definition at line 250 of file mtQueue.ipp.
Referenced by lunchbox::MTQueue< T, S >::getMaxSize().
void lunchbox::MTQueue< T, S >::pushFront | ( | const std::vector< T > & | elements | ) |
Push a vector of elements to the front of the queue.
Definition at line 260 of file mtQueue.ipp.
void lunchbox::MTQueue< T, S >::setMaxSize | ( | const size_t | maxSize | ) |
Set the new maximum size of the queue.
If the new maximum size is less the current size of the queue, this call will block until the queue reaches the new maximum size.
Definition at line 49 of file mtQueue.ipp.
Referenced by lunchbox::MTQueue< T, S >::getSize().
bool lunchbox::MTQueue< T, S >::timedPop | ( | const unsigned | timeout, |
T & | element | ||
) |
Retrieve and pop the front element from the queue.
timeout | the timeout |
element | the element returned |
Definition at line 88 of file mtQueue.ipp.
Referenced by lunchbox::MTQueue< T, S >::getMaxSize().
std::vector< T > lunchbox::MTQueue< T, S >::timedPopRange | ( | const unsigned | timeout, |
const size_t | minimum = 1 , |
||
const size_t | maximum = S |
||
) |
Retrieve a number of items from the front of the queue.
Between minimum and maximum number of items are returned in a vector. If the queue has less than minimum number of elements on timeout, the result vector is empty. The method returns as soon as there are at least minimum elements available, i.e., it does not wait for the maximum to be reached.
Note that this method might block up to 'minimum * timeout' milliseconds, that is, the timeout defines the time to wait for an update on the queue.
timeout | the timeout to wait for an update |
minimum | the minimum number of items to retrieve |
maximum | the maximum number of items to retrieve |
Definition at line 103 of file mtQueue.ipp.
References LB_MIN.
Referenced by lunchbox::MTQueue< T, S >::getMaxSize().
bool lunchbox::MTQueue< T, S >::tryPop | ( | T & | result | ) |
Retrieve and pop the front element from the queue if it is not empty.
result | the front value or unmodified. |
Definition at line 126 of file mtQueue.ipp.
Referenced by lunchbox::MTQueue< T, S >::getMaxSize().
void lunchbox::MTQueue< T, S >::tryPop | ( | const size_t | num, |
std::vector< T > & | result | ||
) |
Try to retrieve a number of items from the front of the queue.
Between zero and the given number of items are appended to the vector.
num | the maximum number of items to retrieve |
result | the front value or unmodified. |
Definition at line 139 of file mtQueue.ipp.
References LB_MIN.
size_t lunchbox::MTQueue< T, S >::waitSize | ( | const size_t | minSize | ) | const |
Wait for the size to be at least the number of given elements.
Definition at line 59 of file mtQueue.ipp.
Referenced by lunchbox::MTQueue< T, S >::getMaxSize().