libcommonc++  0.7
BoundedQueue.h++
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2  commonc++ - A C++ Common Class Library
3  Copyright (C) 2005-2014 Mark A Lindner
4 
5  This file is part of commonc++.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public
18  License along with this library; if not, write to the Free
19  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  ---------------------------------------------------------------------------
21 */
22 
23 #ifndef __ccxx_BoundedQueue_hxx
24 #define __ccxx_BoundedQueue_hxx
25 
26 #include <commonc++/Common.h++>
29 #include <commonc++/Mutex.h++>
30 #include <commonc++/ScopedLock.h++>
31 #include <commonc++/System.h++>
33 
34 #include <deque>
35 
36 namespace ccxx {
37 
44 template <typename T> class BoundedQueue
45 {
46  public:
47 
54  BoundedQueue(uint_t capacity);
55 
57  virtual ~BoundedQueue();
58 
60  void clear();
61 
67  void reset();
68 
77  void put(T item);
78 
90  void tryPut(T item, timespan_ms_t timeout = 0);
91 
100  T take();
101 
113  T tryTake(timespan_ms_t timeout = 0);
114 
119  uint_t getSize() const;
120 
125  inline uint_t getCapacity() const
126  { return(_capacity); }
127 
137  void setCapacity(uint_t capacity);
138 
143  void interrupt();
144 
149  void shutdown();
150 
152  inline bool isShutdown() const
153  { return(_terminated); }
154 
155  private:
156 
157  uint_t _capacity;
158  std::deque<T> _queue;
159  mutable Mutex _mutex;
160  ConditionVar _condP;
161  ConditionVar _condC;
162  bool _terminated;
163 };
164 
165 #include <commonc++/BoundedQueueImpl.h++>
166 
167 } // namespace ccxx
168 
169 #endif // __ccxx_BoundedQueue_hxx
T tryTake(timespan_ms_t timeout=0)
Take an item from the queue.
BoundedQueue(uint_t capacity)
Construct a new BoundedQueue.
T take()
Take an item from the queue.
void reset()
Reset the queue.
bool isShutdown() const
Test if the queue has been shut down.
Definition: BoundedQueue.h++:152
void put(T item)
Put an item in the queue.
void setCapacity(uint_t capacity)
Change the capacity of the queue.
unsigned int uint_t
An alias for unsigned int.
Definition: Integers.h++:74
void shutdown()
Shut down the queue.
A mutual-exclusion lock.
Definition: Mutex.h++:49
virtual ~BoundedQueue()
Destructor.
void tryPut(T item, timespan_ms_t timeout=0)
Put an item in the queue.
void interrupt()
Interrupt the queue.
uint_t getCapacity() const
Get the capacity of the queue, that is, the maximum number of items that the queue can hold...
Definition: BoundedQueue.h++:125
int timespan_ms_t
A timespan expressed in milliseconds.
Definition: Integers.h++:104
uint_t getSize() const
Get the size of the queue, that is, the number of items currently in the queue.
void clear()
Clear the queue.
Definition: AllocationMap.c++:25
A condition variable – a synchronization mechanism used to coordinate the actions of multiple thread...
Definition: ConditionVar.h++:45
A bounded, threadsafe FIFO processing queue.
Definition: BoundedQueue.h++:44