yat  0.14.5pre
PriorityQueue.h
1 #ifndef theplu_yat_utility_priority_queue
2 #define theplu_yat_utility_priority_queue
3 
4 // $Id: PriorityQueue.h 3550 2017-01-03 05:41:02Z peter $
5 //
6 // Copyright (C) 2015, 2016 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 "yat_assert.h"
22 
23 #include <boost/thread.hpp>
24 
25 #include <set>
26 #include <utility>
27 
28 namespace theplu {
29 namespace yat {
30 namespace utility {
31 
53  template<typename T, class Compare = std::less<T> >
55  {
56  public:
58  typedef T value_type;
59 
63  typedef typename std::set<T, Compare>::size_type size_type;
64 
68  PriorityQueue(void) {}
69 
73  explicit PriorityQueue(const Compare& comp) : q_(comp) {}
74 
80  PriorityQueue(const PriorityQueue& other) : q_(other.q_) {}
81 
85  bool empty(void) const
86  {
87  boost::unique_lock<boost::mutex> lock(mutex_);
88  return q_.empty();
89  } // lock is released here
90 
91 
99  void pop(T& value)
100  {
101  boost::unique_lock<boost::mutex> lock(mutex_);
102  while (q_.empty())
103  condition_.wait(lock);
104  pop(value, lock);
105  } // lock is released here
106 
107 
111  void push(const T& t)
112  {
113  boost::unique_lock<boost::mutex> lock(mutex_);
114  q_.insert(t);
115  lock.unlock(); // unlock the mutex
116 
117  // Notify others that data is ready after we have unlocked
118  condition_.notify_one();
119  }
120 
121 
125  size_type size(void) const
126  {
127  boost::unique_lock<boost::mutex> lock(mutex_);
128  return q_.size();
129  } // lock is released here
130 
131 
136  bool try_pop(T& value)
137  {
138  boost::unique_lock<boost::mutex> lock(mutex_);
139  if (q_.empty())
140  return false;
141  pop(value, lock);
142  return true;
143  } // lock is released here
144 
145 
152  {
153  boost::unique_lock<boost::mutex> lock(mutex_);
154  q_ = lhs.q();
155  return *this;
156  } // lock is released here
157 
158  private:
159  // never access this container directly from another object, use
160  // q() instead
161  std::set<T, Compare> q_;
162  mutable boost::mutex mutex_;
163  boost::condition_variable condition_;
164 
165  void pop(T& value, const boost::unique_lock<boost::mutex>& lock)
166  {
167  // The obvious choice would be to create a temp copy of front,
168  // pop the queue and then return by-value. This is, however,
169  // dangerous because if the copy constructor throws, the queue
170  // has been popped and the element is lost. Instead we choose to
171  // return via passed reference.
172  typename std::set<T, Compare>::iterator it = q_.end();
173  YAT_ASSERT(q_.size());
174  --it;
175  value = *it;
176  q_.erase(it);
177  }
178 
179  // thread safe way to access nderlying data
180  const std::set<T, Compare>& q(void) const
181  {
182  boost::unique_lock<boost::mutex> lock(mutex_);
183  return q_;
184  } // lock is released here
185  };
186 
187 }}}
188 #endif
PriorityQueue(const PriorityQueue &other)
Copy constructor.
Definition: PriorityQueue.h:80
T value_type
Type of object stored in Queue.
Definition: PriorityQueue.h:58
Multi-thread safe priority queue.
Definition: PriorityQueue.h:54
PriorityQueue(void)
Create a PriorityQueue with no elements.
Definition: PriorityQueue.h:68
size_type size(void) const
Definition: PriorityQueue.h:125
bool try_pop(T &value)
Definition: PriorityQueue.h:136
bool empty(void) const
Definition: PriorityQueue.h:85
PriorityQueue & operator=(const PriorityQueue &lhs)
assignment operator
Definition: PriorityQueue.h:151
std::set< T, Compare >::size_type size_type
Definition: PriorityQueue.h:63
PriorityQueue(const Compare &comp)
Create a PriorityQueue with comp as Compare functor.
Definition: PriorityQueue.h:73
void push(const T &t)
insert an element into container
Definition: PriorityQueue.h:111
void pop(T &value)
access next element is queue
Definition: PriorityQueue.h:99

Generated on Tue Sep 26 2017 02:33:29 for yat by  doxygen 1.8.5