shared_queue.h
Go to the documentation of this file.
1 
31 #ifndef SHAREDQUEUE_H
32 #define SHAREDQUEUE_H
33 
34 #include <atomic>
35 #include <mutex>
36 #include <queue>
37 
38 template <typename T>
39 class shared_queue {
40 public:
43 
44  shared_queue(const shared_queue& other) {
45  std::lock_guard<std::mutex> other_lock(other.queue_mutex_);
46  std::lock_guard<std::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  std::lock_guard<std::mutex> other_lock(other.queue_mutex_);
57  std::lock_guard<std::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  std::lock_guard<std::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  std::lock_guard<std::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  std::lock_guard<std::mutex> lock(queue_mutex_);
87  return internal_queue_.front();
88  };
89 
90  const T& front() const {
91  std::lock_guard<std::mutex> lock(queue_mutex_);
92  return internal_queue_.front();
93  };
94 
95  T front_pop() {
96  std::lock_guard<std::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  std::lock_guard<std::mutex> lock(queue_mutex_);
107  internal_queue_.pop();
108 
109  queue_empty_ = internal_queue_.empty();
110  };
111 
112  bool empty() const {
113  std::lock_guard<std::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  std::lock_guard<std::mutex> lock(queue_mutex_);
121  return internal_queue_.size();
122  }
123 
124 private:
125  mutable std::mutex queue_mutex_;
126  std::atomic<bool> queue_empty_;
127  std::queue<T> internal_queue_;
128 };
129 
130 #endif
shared_queue::internal_queue_
std::queue< T > internal_queue_
Definition: shared_queue.h:127
shared_queue::shared_queue
shared_queue()
Definition: shared_queue.h:41
shared_queue::fast_empty
bool fast_empty() const
Definition: shared_queue.h:117
shared_queue::push
void push(const T &value)
Definition: shared_queue.h:68
shared_queue::empty
bool empty() const
Definition: shared_queue.h:112
shared_queue::queue_mutex_
std::mutex queue_mutex_
Definition: shared_queue.h:125
shared_queue::pop
void pop()
Definition: shared_queue.h:105
shared_queue::queue_empty_
std::atomic< bool > queue_empty_
Definition: shared_queue.h:126
shared_queue::push
void push(const std::vector< T > &values)
Definition: shared_queue.h:75
shared_queue::shared_queue
shared_queue(const shared_queue &other)
Definition: shared_queue.h:44
shared_queue::front
const T & front() const
Definition: shared_queue.h:90
shared_queue::size
size_t size() const
Definition: shared_queue.h:119
shared_queue::front
T & front()
Definition: shared_queue.h:85
shared_queue::front_pop
T front_pop()
Definition: shared_queue.h:95
values
std::vector< double > values
shared_queue::operator=
shared_queue & operator=(const shared_queue &other)
Definition: shared_queue.h:55
shared_queue::~shared_queue
~shared_queue()
Definition: shared_queue.h:42
shared_queue
Definition: shared_queue.h:39


ubiquity_motor
Author(s):
autogenerated on Thu Nov 16 2023 03:30:55