21 template <
typename T,
size_t S>
27 std::unique_lock<std::mutex> fromLock(from._mutex);
28 std::deque<T> copy = from._queue;
29 const size_t maxSize = from._maxSize;
32 std::unique_lock<std::mutex> lock(_mutex);
35 _condition.notify_all();
39 template <
typename T,
size_t S>
42 std::unique_lock<std::mutex> lock(_mutex);
43 _condition.wait(lock, [&] {
return _queue.size() > index; });
48 template <
typename T,
size_t S>
51 std::unique_lock<std::mutex> lock(_mutex);
52 _condition.wait(lock, [&] {
return _queue.size() <= maxSize; });
55 _condition.notify_all();
58 template <
typename T,
size_t S>
61 LBASSERT(minSize <= _maxSize);
62 std::unique_lock<std::mutex> lock(_mutex);
63 _condition.wait(lock, [&] {
return _queue.size() >= minSize; });
67 template <
typename T,
size_t S>
70 std::unique_lock<std::mutex> lock(_mutex);
72 _condition.notify_all();
75 template <
typename T,
size_t S>
78 std::unique_lock<std::mutex> lock(_mutex);
79 _condition.wait(lock, [&] {
return !_queue.empty(); });
81 T element = _queue.front();
83 _condition.notify_all();
87 template <
typename T,
size_t S>
90 std::unique_lock<std::mutex> lock(_mutex);
91 _condition.wait_for(lock, std::chrono::milliseconds(timeout),
92 [&] {
return !_queue.empty(); });
96 element = _queue.front();
98 _condition.notify_all();
102 template <
typename T,
size_t S>
104 const size_t minimum,
105 const size_t maximum)
107 std::vector<T> result;
109 std::unique_lock<std::mutex> lock(_mutex);
110 _condition.wait_for(lock, std::chrono::milliseconds(timeout),
111 [&] {
return _queue.size() >= minimum; });
112 if (_queue.size() < minimum)
115 const size_t size =
LB_MIN(maximum, _queue.size());
117 result.reserve(size);
118 result.insert(result.end(), _queue.begin(), _queue.begin() + size);
119 _queue.erase(_queue.begin(), _queue.begin() + size);
121 _condition.notify_all();
125 template <
typename T,
size_t S>
128 std::unique_lock<std::mutex> lock(_mutex);
132 result = _queue.front();
134 _condition.notify_all();
138 template <
typename T,
size_t S>
141 std::unique_lock<std::mutex> lock(_mutex);
142 const size_t size =
LB_MIN(num, _queue.size());
145 result.reserve(result.size() + size);
146 for (
size_t i = 0; i < size; ++i)
148 result.push_back(_queue.front());
151 _condition.notify_all();
156 template <
typename T,
size_t S>
175 void setHeight(
const size_t height) { height_ = height; }
178 template <
typename T,
size_t S>
181 LBASSERT(barrier.height_ > 0)
183 std::unique_lock<std::mutex> lock(_mutex);
185 _condition.wait(lock, [&] {
186 return !_queue.empty() || barrier.waiting_ >= barrier.height_;
191 LBASSERT(barrier.waiting_ == barrier.height_);
192 _condition.notify_all();
196 element = _queue.front();
199 _condition.notify_all();
203 template <
typename T,
size_t S>
206 std::unique_lock<std::mutex> lock(_mutex);
211 result = _queue.front();
215 template <
typename T,
size_t S>
218 std::unique_lock<std::mutex> lock(_mutex);
223 result = _queue.back();
227 template <
typename T,
size_t S>
230 std::unique_lock<std::mutex> lock(_mutex);
231 _condition.wait(lock, [&] {
return _queue.size() < _maxSize; });
232 _queue.push_back(element);
233 _condition.notify_all();
236 template <
typename T,
size_t S>
239 std::unique_lock<std::mutex> lock(_mutex);
240 LBASSERT(elements.size() <= _maxSize);
241 _condition.wait(lock, [&] {
242 return (_maxSize - _queue.size()) >= elements.size();
245 _queue.insert(_queue.end(), elements.begin(), elements.end());
246 _condition.notify_all();
249 template <
typename T,
size_t S>
252 std::unique_lock<std::mutex> lock(_mutex);
253 _condition.wait(lock, [&] {
return _queue.size() < _maxSize; });
255 _queue.push_front(element);
256 _condition.notify_all();
259 template <
typename T,
size_t S>
262 std::unique_lock<std::mutex> lock(_mutex);
263 LBASSERT(elements.size() <= _maxSize);
264 _condition.wait(lock, [&] {
265 return (_maxSize - _queue.size()) >= elements.size();
268 _queue.insert(_queue.begin(), elements.begin(), elements.end());
269 _condition.notify_all();
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.
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
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.