yat  0.12.3pre
Queue.h
1 #ifndef theplu_yat_utility_queue
2 #define theplu_yat_utility_queue
3 
4 // $Id: Queue.h 3210 2014-05-05 06:10:02Z peter $
5 //
6 // Copyright (C) 2013, 2014 Peter Johansson
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21 #include <boost/thread.hpp>
22 
23 #include <deque>
24 #include <utility>
25 
26 namespace theplu {
27 namespace yat {
28 namespace utility {
29 
50  template<typename T>
51  class Queue
52  {
53  public:
55  typedef T value_type;
56 
60  typedef typename std::deque<T>::size_type size_type;
61 
65  Queue(void) {}
66 
72  Queue(const Queue& other) : q_(other.q_) {}
73 
77  bool empty(void) const
78  {
79  boost::unique_lock<boost::mutex> lock(mutex_);
80  return q_.empty();
81  } // lock is released here
82 
83 
91  void pop(T& value)
92  {
93  boost::unique_lock<boost::mutex> lock(mutex_);
94  while (q_.empty())
95  condition_.wait(lock);
96  // The obvious choice would be to create a temp copy of front,
97  // pop the queue and then return by-value. This is, however,
98  // dangerous becasue if the copy constructor throws, the queue
99  // has been popped and the element is lost. Instead we choose to
100  // pass via passed reference.
101  value = q_.front();
102  q_.pop_front();
103  } // lock is released here
104 
105 
109  void push(const T& t)
110  {
111  boost::unique_lock<boost::mutex> lock(mutex_);
112  q_.push_back(t);
113  lock.unlock(); // unlock the mutex
114 
115  // Notify others that data is ready after we have unlocked
116  condition_.notify_one();
117  }
118 
119 
123  size_type size(void) const
124  {
125  boost::unique_lock<boost::mutex> lock(mutex_);
126  return q_.size();
127  } // lock is released here
128 
129 
134  bool try_pop(T& value)
135  {
136  boost::unique_lock<boost::mutex> lock(mutex_);
137  if (q_.empty())
138  return false;
139  value = q_.front();
140  q_.pop_front();
141  return true;
142  } // lock is released here
143 
144 
150  Queue& operator=(const Queue& lhs)
151  {
152  q_ = lhs.q_;
153  return *this;
154  }
155 
156  private:
157  std::deque<T> q_;
158  mutable boost::mutex mutex_;
159  boost::condition_variable condition_;
160  };
161 
162 }}}
163 #endif
std::deque< T >::size_type size_type
Definition: Queue.h:60
bool empty(void) const
Definition: Queue.h:77
bool try_pop(T &value)
Definition: Queue.h:134
Queue(void)
Create a Queue with no elements.
Definition: Queue.h:65
Multi-thread safe queue.
Definition: Queue.h:51
Queue(const Queue &other)
Copy constructor.
Definition: Queue.h:72
T value_type
Type of object stored in Queue.
Definition: Queue.h:55
void pop(T &value)
access next element is queue
Definition: Queue.h:91
void push(const T &t)
insert an element into container
Definition: Queue.h:109
size_type size(void) const
Definition: Queue.h:123
Queue & operator=(const Queue &lhs)
assignment operator
Definition: Queue.h:150

Generated on Mon Jun 1 2015 12:29:52 for yat by  doxygen 1.8.5