Lunchbox  1.16.0
Multi-threaded C++ toolbox library for all application developers creating high-performance multi-threaded programs.
lfQueue.ipp
1 
2 /* Copyright (c) 2010-2013, 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 namespace lunchbox
19 {
20 template <typename T>
22 {
23  LB_TS_SCOPED(_reader);
24  _readPos = 0;
25  _writePos = 0;
26 }
27 
28 template <typename T>
29 void LFQueue<T>::resize(const int32_t size)
30 {
31  LBASSERT(isEmpty());
32  _readPos = 0;
33  _writePos = 0;
34  _data.resize(size + 1);
35 }
36 
37 template <typename T>
38 bool LFQueue<T>::pop(T& result)
39 {
40  LB_TS_SCOPED(_reader);
41  if (_readPos == _writePos)
42  return false;
43 
44  result = _data[_readPos];
45  _readPos = (_readPos + 1) % _data.size();
46  return true;
47 }
48 
49 template <typename T>
50 bool LFQueue<T>::getFront(T& result)
51 {
52  LB_TS_SCOPED(_reader);
53  if (_readPos == _writePos)
54  return false;
55 
56  result = _data[_readPos];
57  return true;
58 }
59 
60 template <typename T>
61 bool LFQueue<T>::push(const T& element)
62 {
63  LB_TS_SCOPED(_writer);
64  int32_t nextPos = (_writePos + 1) % _data.size();
65  if (nextPos == _readPos)
66  return false;
67 
68  _data[_writePos] = element;
69  _writePos = nextPos;
70  return true;
71 }
72 }
bool pop(T &result)
Retrieve and pop the front element from the queue.
Definition: lfQueue.ipp:38
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
Abstraction layer and common utilities for multi-threaded programming.
Definition: algorithm.h:29
bool push(const T &element)
Push a new element to the back of the queue.
Definition: lfQueue.ipp:61