Lunchbox  1.16.0
Multi-threaded C++ toolbox library for all application developers creating high-performance multi-threaded programs.
mtQueue.h
1 
2 /* Copyright (c) 2005-2017, Stefan Eilemann <eile@equalizergraphics.com>
3  * Daniel Nachbaur <danielnachbaur@gmail.com>
4  *
5  * This library is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU Lesser General Public License version 2.1 as published
7  * by the Free Software Foundation.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #ifndef LUNCHBOX_MTQUEUE_H
20 #define LUNCHBOX_MTQUEUE_H
21 
22 #include <lunchbox/debug.h>
23 
24 #include <algorithm>
25 #include <condition_variable>
26 #include <limits.h>
27 #include <mutex>
28 #include <queue>
29 #include <string.h>
30 
31 namespace lunchbox
32 {
44 template <typename T, size_t S = ULONG_MAX>
45 class MTQueue
46 // S = std::numeric_limits< size_t >::max() does not work:
47 // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=6424
48 {
49 public:
50  class Group;
51  typedef T value_type;
52 
54  explicit MTQueue(const size_t maxSize = S)
55  : _maxSize(maxSize)
56  {
57  }
58 
60  MTQueue(const MTQueue<T, S>& from) { *this = from; }
62  ~MTQueue() {}
65 
70  const T& operator[](const size_t index) const;
71 
73  bool isEmpty() const { return _queue.empty(); }
75  size_t getSize() const { return _queue.size(); }
84  void setMaxSize(const size_t maxSize);
85 
87  size_t getMaxSize() const { return _maxSize; }
94  size_t waitSize(const size_t minSize) const;
95 
97  void clear();
98 
103  T pop();
104 
113  bool timedPop(const unsigned timeout, T& element);
114 
133  std::vector<T> timedPopRange(const unsigned timeout,
134  const size_t minimum = 1,
135  const size_t maximum = S);
136 
145  bool tryPop(T& result);
146 
158  void tryPop(const size_t num, std::vector<T>& result);
159 
173  bool popBarrier(T& result, Group& barrier);
174 
181  bool getFront(T& result) const;
182 
189  bool getBack(T& result) const;
190 
192  void push(const T& element);
193 
195  void push(const std::vector<T>& elements);
196 
198  void pushFront(const T& element);
199 
201  void pushFront(const std::vector<T>& elements);
202 
205  void push_back(const T& element) { push(element); }
206  bool empty() const { return isEmpty(); }
208 
209 private:
210  MTQueue(MTQueue<T, S>&&) = delete;
212 
213  std::deque<T> _queue;
214  mutable std::mutex _mutex;
215  mutable std::condition_variable _condition;
216  size_t _maxSize;
217 };
218 }
219 
220 #include "mtQueue.ipp" // template implementation
221 
222 #endif // LUNCHBOX_MTQUEUE_H
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.
Definition: mtQueue.ipp:103
bool tryPop(T &result)
Retrieve and pop the front element from the queue if it is not empty.
Definition: mtQueue.ipp:126
MTQueue< T, S > & operator=(const MTQueue< T, S > &from)
Assign the values of another queue.
Definition: mtQueue.ipp:22
void pushFront(const T &element)
Push a new element to the front of the queue.
Definition: mtQueue.ipp:250
size_t waitSize(const size_t minSize) const
Wait for the size to be at least the number of given elements.
Definition: mtQueue.ipp:59
void clear()
Reset (empty) the queue.
Definition: mtQueue.ipp:68
A thread-safe queue with a blocking read access.
Definition: mtQueue.h:45
const T & operator[](const size_t index) const
Retrieve the requested element from the queue, may block.
Definition: mtQueue.ipp:40
~MTQueue()
Destruct this Queue.
Definition: mtQueue.h:62
bool getFront(T &result) const
Definition: mtQueue.ipp:204
bool popBarrier(T &result, Group &barrier)
Retrieve the front element, or abort if the barrier is reached.
Definition: mtQueue.ipp:179
bool isEmpty() const
Definition: mtQueue.h:73
void setMaxSize(const size_t maxSize)
Set the new maximum size of the queue.
Definition: mtQueue.ipp:49
bool getBack(T &result) const
Definition: mtQueue.ipp:216
size_t getSize() const
Definition: mtQueue.h:75
bool timedPop(const unsigned timeout, T &element)
Retrieve and pop the front element from the queue.
Definition: mtQueue.ipp:88
MTQueue(const MTQueue< T, S > &from)
Construct a copy of a queue.
Definition: mtQueue.h:60
Group descriptor for popBarrier().
Definition: mtQueue.ipp:157
T pop()
Retrieve and pop the front element from the queue, may block.
Definition: mtQueue.ipp:76
Abstraction layer and common utilities for multi-threaded programming.
Definition: algorithm.h:29
MTQueue(const size_t maxSize=S)
Construct a new queue.
Definition: mtQueue.h:54
size_t getMaxSize() const
Definition: mtQueue.h:87
void push(const T &element)
Push a new element to the back of the queue.
Definition: mtQueue.ipp:228