shared_queue.h
Go to the documentation of this file.
1 
31 #ifndef SHAREDQUEUE_H
32 #define SHAREDQUEUE_H
33 
34 #include <boost/atomic.hpp>
35 #include <boost/thread.hpp>
36 #include <queue>
37 
38 template <typename T>
39 class shared_queue {
40 public:
43 
44  shared_queue(const shared_queue& other) {
45  boost::lock_guard<boost::mutex> other_lock(other.queue_mutex_);
46  boost::lock_guard<boost::mutex> this_lock(queue_mutex_);
47 
48  // Copy is not atomic
49  bool qe = other.queue_empty_;
50  queue_empty_ = qe;
51 
53  };
54 
56  boost::lock_guard<boost::mutex> other_lock(other.queue_mutex_);
57  boost::lock_guard<boost::mutex> this_lock(queue_mutex_);
58 
59  // Copy is not atomic
60  bool qe = other.queue_empty_;
61  queue_empty_ = qe;
62 
64 
65  return *this;
66  };
67 
68  void push(const T& value) {
69  boost::lock_guard<boost::mutex> lock(queue_mutex_);
70  internal_queue_.push(value);
71 
72  queue_empty_ = internal_queue_.empty();
73  };
74 
75  void push(const std::vector<T>& values) {
76  boost::lock_guard<boost::mutex> lock(queue_mutex_);
77 
78  for (typename std::vector<T>::const_iterator it = values.begin();
79  it != values.end(); ++it) {
80  internal_queue_.push(*it);
81  queue_empty_ = internal_queue_.empty();
82  }
83  };
84 
85  T& front() {
86  boost::lock_guard<boost::mutex> lock(queue_mutex_);
87  return internal_queue_.front();
88  };
89 
90  const T& front() const {
91  boost::lock_guard<boost::mutex> lock(queue_mutex_);
92  return internal_queue_.front();
93  };
94 
95  T front_pop() {
96  boost::lock_guard<boost::mutex> lock(queue_mutex_);
97 
98  T value = internal_queue_.front();
99  internal_queue_.pop();
100  queue_empty_ = internal_queue_.empty();
101 
102  return value;
103  };
104 
105  void pop() {
106  boost::lock_guard<boost::mutex> lock(queue_mutex_);
107  internal_queue_.pop();
108 
109  queue_empty_ = internal_queue_.empty();
110  };
111 
112  bool empty() const {
113  boost::lock_guard<boost::mutex> lock(queue_mutex_);
114  return internal_queue_.empty();
115  }
116 
117  bool fast_empty() const { return queue_empty_; }
118 
119  size_t size() const {
120  boost::lock_guard<boost::mutex> lock(queue_mutex_);
121  return internal_queue_.size();
122  }
123 
124 private:
125  mutable boost::mutex queue_mutex_;
126  boost::atomic<bool> queue_empty_;
127  std::queue<T> internal_queue_;
128 };
129 
130 #endif
std::queue< T > internal_queue_
Definition: shared_queue.h:127
shared_queue(const shared_queue &other)
Definition: shared_queue.h:44
boost::mutex queue_mutex_
Definition: shared_queue.h:125
void push(const T &value)
Definition: shared_queue.h:68
boost::atomic< bool > queue_empty_
Definition: shared_queue.h:126
void push(const std::vector< T > &values)
Definition: shared_queue.h:75
bool fast_empty() const
Definition: shared_queue.h:117
size_t size() const
Definition: shared_queue.h:119
bool empty() const
Definition: shared_queue.h:112
shared_queue & operator=(const shared_queue &other)
Definition: shared_queue.h:55
const T & front() const
Definition: shared_queue.h:90


ubiquity_motor
Author(s):
autogenerated on Mon Jun 10 2019 15:37:24