Lunchbox
1.10.0
|
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 |
|
inline |
|
inline |
void lunchbox::MTQueue< T, S >::clear | ( | ) |
bool lunchbox::MTQueue< T, S >::getBack | ( | T & | result | ) | const |
result | the last value or unmodified. |
Definition at line 242 of file mtQueue.ipp.
bool lunchbox::MTQueue< T, S >::getFront | ( | T & | result | ) | const |
result | the front value or unmodified. |
Definition at line 227 of file mtQueue.ipp.
|
inline |
|
inline |
|
inline |
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.
References lunchbox::Condition::lock(), and lunchbox::Condition::unlock().
const T & lunchbox::MTQueue< T, S >::operator[] | ( | const size_t | index | ) | const |
Retrieve the requested element from the queue, may block.
Definition at line 41 of file mtQueue.ipp.
T lunchbox::MTQueue< T, S >::pop | ( | ) |
Retrieve and pop the front element from the queue, may block.
Definition at line 86 of file mtQueue.ipp.
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 200 of file mtQueue.ipp.
void lunchbox::MTQueue< T, S >::push | ( | const T & | element | ) |
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 268 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 280 of file mtQueue.ipp.
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 291 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 54 of file mtQueue.ipp.
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 101 of file mtQueue.ipp.
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 121 of file mtQueue.ipp.
References LB_MIN.
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 147 of file mtQueue.ipp.
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 164 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 65 of file mtQueue.ipp.