Lunchbox  1.14.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 > class LFQueue : public boost::noncopyable
43 {
44 public:
46  explicit LFQueue( const int32_t size )
47  : _data( size + 1 ), _readPos( 0 ), _writePos( 0 ) {}
48 
50  ~LFQueue() {}
51 
53  bool isEmpty() const { return _readPos == _writePos; }
54 
56  void clear();
57 
64  void resize( const int32_t size );
65 
74  bool pop( T& result );
75 
84  bool getFront( T& result );
85 
93  bool push( const T& element );
94 
99  size_t getCapacity() const { return _data.size() - 1; }
100 
101 private:
102  std::vector< T > _data;
103  a_int32_t _readPos;
104  a_int32_t _writePos;
105 
106  LB_TS_VAR( _reader );
107  LB_TS_VAR( _writer );
108 };
109 }
110 
111 #include "lfQueue.ipp" // template implementation
112 
113 #endif // LUNCHBOX_LFQUEUE_H
bool pop(T &result)
Retrieve and pop the front element from the queue.
Definition: lfQueue.ipp:35
LFQueue(const int32_t size)
Construct a new queue.
Definition: lfQueue.h:46
void clear()
Reset (empty) the queue.
Definition: lfQueue.ipp:20
void resize(const int32_t size)
Resize and reset the queue.
Definition: lfQueue.ipp:27
bool getFront(T &result)
Retrieve the front element from the queue.
Definition: lfQueue.ipp:46
A thread-safe, lock-free queue with non-blocking access.
Definition: lfQueue.h:42
size_t getCapacity() const
Definition: lfQueue.h:99
Abstraction layer and common utilities for multi-threaded programming.
Definition: algorithm.h:32
~LFQueue()
Destruct this queue.
Definition: lfQueue.h:50
bool isEmpty() const
Definition: lfQueue.h:53
bool push(const T &element)
Push a new element to the back of the queue.
Definition: lfQueue.ipp:56