yat  0.15.2pre
Queue.h
1 #ifndef theplu_yat_utility_queue
2 #define theplu_yat_utility_queue
3 
4 // $Id: Queue.h 3689 2017-09-12 03:46:44Z peter $
5 //
6 // Copyright (C) 2013, 2014, 2016, 2017 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 "config_public.h"
22 
23 #include <boost/thread.hpp>
24 
25 #include <deque>
26 #include <utility>
27 
28 namespace theplu {
29 namespace yat {
30 namespace utility {
31 
56  template<typename T>
57  class Queue
58  {
59  public:
61  typedef T value_type;
62 
66  typedef typename std::deque<T>::size_type size_type;
67 
71  Queue(void) {}
72 
78  Queue(const Queue& other) : q_(other.q()) {}
79 
85  void clear(void)
86  {
87  boost::unique_lock<boost::mutex> lock(mutex_);
88  return q_.clear();
89  }
90 
94  bool empty(void) const
95  {
96  boost::unique_lock<boost::mutex> lock(mutex_);
97  return q_.empty();
98  } // lock is released here
99 
100 
108  void pop(T& value)
109  {
110  boost::unique_lock<boost::mutex> lock(mutex_);
111  while (q_.empty())
112  condition_.wait(lock);
113  // The obvious choice would be to create a temp copy of front,
114  // pop the queue and then return by-value. This is, however,
115  // dangerous becasue if the copy constructor throws, the queue
116  // has been popped and the element is lost. Instead we choose to
117  // pass via passed reference.
118  value = q_.front();
119  q_.pop_front();
120  } // lock is released here
121 
122 
126  void push(const T& t)
127  {
128  boost::unique_lock<boost::mutex> lock(mutex_);
129  q_.push_back(t);
130  lock.unlock(); // unlock the mutex
131 
132  // Notify others that data is ready after we have unlocked
133  condition_.notify_one();
134  }
135 
136 
137 #ifdef YAT_HAVE_RVALUE
138 
145  void push(T&& t)
146  {
147  boost::unique_lock<boost::mutex> lock(mutex_);
148  q_.push_back(std::move(t));
149  lock.unlock(); // unlock the mutex
150 
151  // Notify others that data is ready after we have unlocked
152  condition_.notify_one();
153  }
154 #endif
155 
156 
160  size_type size(void) const
161  {
162  boost::unique_lock<boost::mutex> lock(mutex_);
163  return q_.size();
164  } // lock is released here
165 
166 
171  bool try_pop(T& value)
172  {
173  boost::unique_lock<boost::mutex> lock(mutex_);
174  if (q_.empty())
175  return false;
176  value = q_.front();
177  q_.pop_front();
178  return true;
179  } // lock is released here
180 
181 
185  Queue& operator=(const Queue& lhs)
186  {
187  boost::unique_lock<boost::mutex> lock(mutex_);
188  q_ = lhs.q();
189  return *this;
190  } // lock is released here
191 
192  private:
193  std::deque<T> q_;
194  mutable boost::mutex mutex_;
195  boost::condition_variable condition_;
196 
197  // thread safe way to access nderlying data
198  const std::deque<T>& q(void) const
199  {
200  boost::unique_lock<boost::mutex> lock(mutex_);
201  return q_;
202  } // lock is released here
203  };
204 
205 }}}
206 #endif
std::deque< T >::size_type size_type
Definition: Queue.h:66
The Department of Theoretical Physics namespace as we define it.
bool empty(void) const
Definition: Queue.h:94
bool try_pop(T &value)
Definition: Queue.h:171
Queue(void)
Create a Queue with no elements.
Definition: Queue.h:71
void clear(void)
clear queue
Definition: Queue.h:85
Multi-thread safe queue.
Definition: Queue.h:57
Queue(const Queue &other)
Copy constructor.
Definition: Queue.h:78
T value_type
Type of object stored in Queue.
Definition: Queue.h:61
void pop(T &value)
access next element in queue
Definition: Queue.h:108
void push(const T &t)
insert an element into container
Definition: Queue.h:126
size_type size(void) const
Definition: Queue.h:160
Queue & operator=(const Queue &lhs)
assignment operator
Definition: Queue.h:185

Generated on Fri Jul 13 2018 02:33:27 for yat by  doxygen 1.8.11