KISSCPP
a C++ library for rapid application development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
threadsafe_queue.hpp
Go to the documentation of this file.
1 // File : threadsafe_queue.hpp
2 // Author: Dirk J. Botha <bothadj@gmail.com>
3 //
4 // This file is part of kisscpp library.
5 //
6 // The kisscpp library is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // The kisscpp library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with the kisscpp library. If not, see <http://www.gnu.org/licenses/>.
18 
19 #ifndef _THREADSAFE_QUEUE_HPP_
20 #define _THREADSAFE_QUEUE_HPP_
21 
22 #include <boost/shared_ptr.hpp>
23 #include <boost/thread/mutex.hpp>
24 #include <boost/thread/locks.hpp>
25 #include "statable_queue.hpp"
26 
27 //--------------------------------------------------------------------------------
28 namespace kisscpp
29 {
30 
31  template <class _qoT, >
32  class ThreadsafeQueue : public StatAbleQueue, public boost::noncopyable
33  {
34  public:
36 
38 
39  void push(boost::shared_ptr<_qoT> p)
40  {
41  boost::lock_guard<boost::mutex> guard(queueMutex);
42  queue->push_back(p);
43  }
44 
45  boost::shared_ptr<_qoT> pop()
46  {
47  boost::lock_guard<boost::mutex> guard(queueMutex);
48  return queue->pop_front();
49  }
50 
51  boost::shared_ptr<_qoT> front()
52  {
53  boost::lock_guard<boost::mutex> guard(queueMutex);
54  return queue->front();
55  }
56 
57  boost::shared_ptr<_qoT> back()
58  {
59  boost::lock_guard<boost::mutex> guard(queueMutex);
60  return queue->back();
61  }
62 
63  bool empty()
64  {
65  boost::lock_guard<boost::mutex> guard(queueMutex);
66  return queue->empty();
67  }
68 
69  size_t size()
70  {
71  boost::lock_guard<boost::mutex> guard(queueMutex);
72  return queue->size();
73  }
74 
75  protected:
76  private:
77  std::deque<boost::shared_ptr<_qoT> > queue;
78  boost::mutex queueMutex;
79  };
80 
81 }
82 #endif
83 
ThreadsafeQueue()
Definition: threadsafe_queue.hpp:35
bool empty()
Definition: threadsafe_queue.hpp:63
size_t size()
Definition: threadsafe_queue.hpp:69
~ThreadsafeQueue()
Definition: threadsafe_queue.hpp:37
boost::shared_ptr< _qoT > back()
Definition: threadsafe_queue.hpp:57
boost::shared_ptr< _qoT > front()
Definition: threadsafe_queue.hpp:51
void push(boost::shared_ptr< _qoT > p)
Definition: threadsafe_queue.hpp:39
Definition: threadsafe_queue.hpp:32
Definition: statable_queue.hpp:8
boost::shared_ptr< _qoT > pop()
Definition: threadsafe_queue.hpp:45