Lunchbox  1.17.0
Multi-threaded C++ toolbox library for all application developers creating high-performance multi-threaded programs.
lfQueue.h
1 
2 /* Copyright (c) 2010-2014, Stefan Eilemann <eile@equalizergraphics.com>
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License version 2.1 as published
6  * by the Free Software Foundation.
7  *
8  * This library is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11  * details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this library; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16  */
17 
18 #ifndef LUNCHBOX_LFQUEUE_H
19 #define LUNCHBOX_LFQUEUE_H
20 
21 #include <lunchbox/atomic.h> // member
22 #include <lunchbox/debug.h> // used in inline method
23 #include <lunchbox/thread.h> // thread-safety checks
24 
25 #include <vector>
26 
27 namespace lunchbox
28 {
42 template <typename T>
43 class LFQueue : public boost::noncopyable
44 {
45 public:
47  explicit LFQueue(const int32_t size)
48  : _data(size + 1)
49  , _readPos(0)
50  , _writePos(0)
51  {
52  }
53 
55  ~LFQueue() {}
57  bool isEmpty() const { return _readPos == _writePos; }
59  void clear();
60 
67  void resize(const int32_t size);
68 
77  bool pop(T& result);
78 
87  bool getFront(T& result);
88 
96  bool push(const T& element);
97 
102  size_t getCapacity() const { return _data.size() - 1; }
103 private:
104  std::vector<T> _data;
105  a_int32_t _readPos;
106  a_int32_t _writePos;
107 
108  LB_TS_VAR(_reader);
109  LB_TS_VAR(_writer);
110 };
111 }
112 
113 #include "lfQueue.ipp" // template implementation
114 
115 #endif // LUNCHBOX_LFQUEUE_H
bool pop(T &result)
Retrieve and pop the front element from the queue.
Definition: lfQueue.ipp:38
LFQueue(const int32_t size)
Construct a new queue.
Definition: lfQueue.h:47
void clear()
Reset (empty) the queue.
Definition: lfQueue.ipp:21
void resize(const int32_t size)
Resize and reset the queue.
Definition: lfQueue.ipp:29
bool getFront(T &result)
Retrieve the front element from the queue.
Definition: lfQueue.ipp:50
A thread-safe, lock-free queue with non-blocking access.
Definition: lfQueue.h:43
size_t getCapacity() const
Definition: lfQueue.h:102
Abstraction layer and common utilities for multi-threaded programming.
Definition: algorithm.h:29
~LFQueue()
Destruct this queue.
Definition: lfQueue.h:55
bool isEmpty() const
Definition: lfQueue.h:57
bool push(const T &element)
Push a new element to the back of the queue.
Definition: lfQueue.ipp:61