FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
SafeQueue.h
1 #/**********************************************************\
2 #Original Author: Anthony Williams
3 #Imported and Adapted By: Richard Bateman
4 #Original Source: http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-queue-using-condition-variables.html
5 #
6 #License: Dual license model; choose one of two:
7 # New BSD License
8 # http://www.opensource.org/licenses/bsd-license.php
9 # - or -
10 # GNU Lesser General Public License, version 2.1
11 # http://www.gnu.org/licenses/lgpl-2.1.html
12 #
13 #Copyright 2008
14 #\**********************************************************/
15 
16 #ifndef H_FB_SafeQueue
17 #define H_FB_SafeQueue
18 
19 #include <boost/thread/mutex.hpp>
20 #include <boost/thread/condition_variable.hpp>
21 #include <queue>
22 
23 namespace FB {
24 
30  template<typename Data>
31  class SafeQueue
32  {
33  private:
34  std::queue<Data> the_queue;
35  mutable boost::mutex the_mutex;
36  boost::condition_variable the_condition_variable;
37  public:
45  void push(Data const& data)
46  {
47  boost::mutex::scoped_lock lock(the_mutex);
48  the_queue.push(data);
49  lock.unlock();
50  the_condition_variable.notify_one();
51  }
52 
60  bool empty() const
61  {
62  boost::mutex::scoped_lock lock(the_mutex);
63  return the_queue.empty();
64  }
65 
75  bool try_pop(Data& popped_value)
76  {
77  boost::mutex::scoped_lock lock(the_mutex);
78  if(the_queue.empty())
79  {
80  return false;
81  }
82 
83  popped_value=the_queue.front();
84  the_queue.pop();
85  return true;
86  }
87 
100  bool timed_wait_and_pop(Data& popped_value, const boost::posix_time::time_duration& duration)
101  {
102  boost::mutex::scoped_lock lock(the_mutex);
103  if(the_queue.empty())
104  {
105  // Wait for the specified duration if no values are there
106  the_condition_variable.timed_wait(lock, duration);
107  }
108 
109  // See if a value was added; if not, we just timed out
110  if(the_queue.empty())
111  {
112  return false;
113  }
114 
115  popped_value=the_queue.front();
116  the_queue.pop();
117  return true;
118  }
127  void wait_and_pop(Data& popped_value)
128  {
129  boost::mutex::scoped_lock lock(the_mutex);
130  while(the_queue.empty())
131  {
132  the_condition_variable.wait(lock);
133  }
134 
135  popped_value=the_queue.front();
136  the_queue.pop();
137  }
138 
139  };
140 };
141 
142 #endif //H_FB_SafeQueue
143 
bool try_pop(Data &popped_value)
Try to pop a value off the front of the queue; if the queue is empty returns false.
Definition: SafeQueue.h:75
bool empty() const
Queries if the queue is empty.
Definition: SafeQueue.h:60
void push(Data const &data)
Pushes an object onto the end of the queue.
Definition: SafeQueue.h:45
Basic thread-safe queue class.
Definition: SafeQueue.h:31
bool timed_wait_and_pop(Data &popped_value, const boost::posix_time::time_duration &duration)
Tries to pop a value off the front of the queue; if the queue is empty it will wait for the specified...
Definition: SafeQueue.h:100
void wait_and_pop(Data &popped_value)
Tries to pop a value off the front of the queue; if the queue is empty it will wait indefinitely unti...
Definition: SafeQueue.h:127