bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
concurrent_queue.h
Go to the documentation of this file.
1#ifndef BITSIM_UTILS_CONCURRENCY_CONCURRENT_QUEUE_H
2#define BITSIM_UTILS_CONCURRENCY_CONCURRENT_QUEUE_H
3
4#include <chrono>
5#include <condition_variable>
6#include <cstddef>
7#include <mutex>
8#include <optional>
9#include <queue>
10#include <utility>
11
12namespace bitsim
13{
15{
16
25template <typename T> class ConcurrentQueue
26{
27 public:
28 using value_type = T;
29
34 bool push(T value);
35
39 std::optional<T> try_pop();
40
45 template <typename Rep, typename Period>
46 std::optional<T> pop_for(std::chrono::duration<Rep, Period> timeout);
47
52 std::optional<T> pop();
53
57 void close();
58
62 bool is_closed() const;
63
67 std::size_t size() const;
68
72 bool empty() const;
73
74 private:
75 std::optional<T> take_front_();
76
77 mutable std::mutex mutex_;
78 std::condition_variable cv_;
79 std::queue<T> queue_;
80 bool closed_{false};
81};
82
83template <typename T> bool ConcurrentQueue<T>::push(T value)
84{
85 {
86 std::lock_guard<std::mutex> lock(mutex_);
87 if (closed_)
88 {
89 return false;
90 }
91 queue_.push(std::move(value));
92 }
93 cv_.notify_one();
94 return true;
95}
96
97template <typename T> std::optional<T> ConcurrentQueue<T>::take_front_()
98{
99 if (queue_.empty())
100 {
101 return std::nullopt;
102 }
103 std::optional<T> value(std::move(queue_.front()));
104 queue_.pop();
105 return value;
106}
107
108template <typename T> std::optional<T> ConcurrentQueue<T>::try_pop()
109{
110 std::lock_guard<std::mutex> lock(mutex_);
111 return take_front_();
112}
113
114template <typename T>
115template <typename Rep, typename Period>
116std::optional<T> ConcurrentQueue<T>::pop_for(std::chrono::duration<Rep, Period> timeout)
117{
118 std::unique_lock<std::mutex> lock(mutex_);
119 cv_.wait_for(lock, timeout, [this] { return closed_ || !queue_.empty(); });
120 return take_front_();
121}
122
123template <typename T> std::optional<T> ConcurrentQueue<T>::pop()
124{
125 std::unique_lock<std::mutex> lock(mutex_);
126 cv_.wait(lock, [this] { return closed_ || !queue_.empty(); });
127 return take_front_();
128}
129
130template <typename T> void ConcurrentQueue<T>::close()
131{
132 {
133 std::lock_guard<std::mutex> lock(mutex_);
134 closed_ = true;
135 }
136 cv_.notify_all();
137}
138
139template <typename T> bool ConcurrentQueue<T>::is_closed() const
140{
141 std::lock_guard<std::mutex> lock(mutex_);
142 return closed_;
143}
144
145template <typename T> std::size_t ConcurrentQueue<T>::size() const
146{
147 std::lock_guard<std::mutex> lock(mutex_);
148 return queue_.size();
149}
150
151template <typename T> bool ConcurrentQueue<T>::empty() const
152{
153 std::lock_guard<std::mutex> lock(mutex_);
154 return queue_.empty();
155}
156
157} // namespace utils::concurrency
158} // namespace bitsim
159
160#endif // BITSIM_UTILS_CONCURRENCY_CONCURRENT_QUEUE_H
A first-in first-out queue that several threads can push to and pop from.
Definition concurrent_queue.h:26
std::optional< T > pop()
Take the item at the front of the queue, waiting until there is one.
Definition concurrent_queue.h:123
bool is_closed() const
Whether close() was called.
Definition concurrent_queue.h:139
void close()
Stop accepting items and wake every thread that waits for one.
Definition concurrent_queue.h:130
std::size_t size() const
The number of items in the queue.
Definition concurrent_queue.h:145
bool empty() const
Whether the queue has no items.
Definition concurrent_queue.h:151
std::optional< T > try_pop()
Take the item at the front of the queue, if there is one, without waiting.
Definition concurrent_queue.h:108
bool push(T value)
Add an item at the back of the queue.
Definition concurrent_queue.h:83
T value_type
Definition concurrent_queue.h:28
std::optional< T > pop_for(std::chrono::duration< Rep, Period > timeout)
Take the item at the front of the queue, waiting at most timeout for one.
Definition concurrent_queue.h:116
std::optional< T > pop()
Take the item at the front of the queue, waiting until there is one.
Definition concurrent_queue.h:123
bool is_closed() const
Whether close() was called.
Definition concurrent_queue.h:139
void close()
Stop accepting items and wake every thread that waits for one.
Definition concurrent_queue.h:130
std::size_t size() const
The number of items in the queue.
Definition concurrent_queue.h:145
bool empty() const
Whether the queue has no items.
Definition concurrent_queue.h:151
std::optional< T > try_pop()
Take the item at the front of the queue, if there is one, without waiting.
Definition concurrent_queue.h:108
bool push(T value)
Add an item at the back of the queue.
Definition concurrent_queue.h:83
std::optional< T > pop_for(std::chrono::duration< Rep, Period > timeout)
Take the item at the front of the queue, waiting at most timeout for one.
Definition concurrent_queue.h:116
Definition concurrent_queue.h:15
Definition dc_motor_backend_base.h:9