yat  0.13.2pre
PriorityQueue.h
1 #ifndef theplu_yat_utility_priority_queue
2 #define theplu_yat_utility_priority_queue
3 
4 // $Id: PriorityQueue.h 3403 2015-03-31 05:18:45Z peter $
5 //
6 // Copyright (C) 2015 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  q_ = lhs.q_;
154  return *this;
155  }
156 
157  private:
158  std::set<T, Compare> q_;
159  mutable boost::mutex mutex_;
160  boost::condition_variable condition_;
161 
162  void pop(T& value, const boost::unique_lock<boost::mutex>& lock)
163  {
164  // The obvious choice would be to create a temp copy of front,
165  // pop the queue and then return by-value. This is, however,
166  // dangerous because if the copy constructor throws, the queue
167  // has been popped and the element is lost. Instead we choose to
168  // return via passed reference.
169  typename std::set<T, Compare>::iterator it = q_.end();
170  YAT_ASSERT(q_.size());
171  --it;
172  value = *it;
173  q_.erase(it);
174  }
175  };
176 
177 }}}
178 #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 Wed Jan 4 2017 02:23:07 for yat by  doxygen 1.8.5