21 template<
typename T,
size_t S >
27 std::deque< T > copy = from._queue;
28 const size_t maxSize = from._maxSize;
40 template<
typename T,
size_t S >
44 while( _queue.size() <= index )
47 LBASSERT( _queue.size() > index );
48 const T& element = _queue[index];
53 template<
typename T,
size_t S >
57 while( _queue.size() > maxSize )
64 template<
typename T,
size_t S >
67 LBASSERT( minSize <= _maxSize );
69 while( _queue.size() < minSize )
71 const size_t size = _queue.size();
76 template<
typename T,
size_t S >
85 template<
typename T,
size_t S >
89 while( _queue.empty( ))
92 LBASSERT( !_queue.empty( ));
93 T element = _queue.front();
100 template<
typename T,
size_t S >
104 while( _queue.empty( ))
106 if( !_cond.timedWait( timeout ))
112 LBASSERT( !_queue.empty( ));
113 element = _queue.front();
120 template<
typename T,
size_t S > std::vector< T >
122 const size_t maximum )
124 std::vector< T > result;
127 while( _queue.size() < minimum )
129 if( !_cond.timedWait( timeout ))
136 const size_t size =
LB_MIN( maximum, _queue.size( ));
138 result.reserve( size );
139 result.insert( result.end(), _queue.begin(), _queue.begin() + size );
140 _queue.erase( _queue.begin(), _queue.begin() + size );
146 template<
typename T,
size_t S >
156 result = _queue.front();
163 template<
typename T,
size_t S >
167 const size_t size =
LB_MIN( num, _queue.size( ));
170 result.reserve( result.size() + size );
171 for(
size_t i = 0; i < size; ++i )
173 result.push_back( _queue.front( ));
193 explicit Group(
const size_t height ) : height_( height ), waiting_( 0 ) {}
196 void setHeight(
const size_t height ) { height_ = height; }
199 template<
typename T,
size_t S >
202 LBASSERT( barrier.height_ > 0 )
206 while( _queue.empty() && barrier.waiting_ < barrier.height_ )
211 LBASSERT( barrier.waiting_ == barrier.height_ );
217 element = _queue.front();
226 template<
typename T,
size_t S >
236 result = _queue.front();
241 template<
typename T,
size_t S >
251 result = _queue.back();
256 template<
typename T,
size_t S >
260 while( _queue.size() >= _maxSize )
262 _queue.push_back( element );
267 template<
typename T,
size_t S >
271 LBASSERT( elements.size() <= _maxSize );
272 while( (_maxSize - _queue.size( )) < elements.size( ))
274 _queue.insert( _queue.end(), elements.begin(), elements.end( ));
279 template<
typename T,
size_t S >
283 while( _queue.size() >= _maxSize )
285 _queue.push_front( element );
290 template<
typename T,
size_t S >
294 LBASSERT( elements.size() <= _maxSize );
295 while( (_maxSize - _queue.size( )) < elements.size( ))
297 _queue.insert(_queue.begin(), elements.begin(), elements.end());
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.
bool tryPop(T &result)
Retrieve and pop the front element from the queue if it is not empty.
void unlock()
Unlock the mutex.
MTQueue< T, S > & operator=(const MTQueue< T, S > &from)
Assign the values of another queue.
void pushFront(const T &element)
Push a new element to the front of the queue.
size_t waitSize(const size_t minSize) const
Wait for the size to be at least the number of given elements.
void clear()
Reset (empty) the queue.
A thread-safe queue with a blocking read access.
const T & operator[](const size_t index) const
Retrieve the requested element from the queue, may block.
Group(const size_t height)
Construct a new group of the given size.
bool getFront(T &result) const
void lock()
Lock the mutex.
bool popBarrier(T &result, Group &barrier)
Retrieve the front element, or abort if the barrier is reached.
#define LB_MIN(a, b)
returns the minimum of two values
void setHeight(const size_t height)
Update the height.
void setMaxSize(const size_t maxSize)
Set the new maximum size of the queue.
bool getBack(T &result) const
bool timedPop(const unsigned timeout, T &element)
Retrieve and pop the front element from the queue.
Group descriptor for popBarrier().
T pop()
Retrieve and pop the front element from the queue, may block.
Abstraction layer and common utilities for multi-threaded programming.
void push(const T &element)
Push a new element to the back of the queue.