1#ifndef BITSIM_UTILS_CONCURRENCY_CONCURRENT_QUEUE_H
2#define BITSIM_UTILS_CONCURRENCY_CONCURRENT_QUEUE_H
5#include <condition_variable>
45 template <
typename Rep,
typename Period>
46 std::optional<T>
pop_for(std::chrono::duration<Rep, Period> timeout);
52 std::optional<T>
pop();
75 std::optional<T> take_front_();
77 mutable std::mutex mutex_;
78 std::condition_variable cv_;
86 std::lock_guard<std::mutex> lock(mutex_);
91 queue_.push(std::move(value));
97template <
typename T> std::optional<T> ConcurrentQueue<T>::take_front_()
103 std::optional<T> value(std::move(queue_.front()));
110 std::lock_guard<std::mutex> lock(mutex_);
111 return take_front_();
115template <
typename Rep,
typename Period>
118 std::unique_lock<std::mutex> lock(mutex_);
119 cv_.wait_for(lock, timeout, [
this] {
return closed_ || !queue_.empty(); });
120 return take_front_();
125 std::unique_lock<std::mutex> lock(mutex_);
126 cv_.wait(lock, [
this] {
return closed_ || !queue_.empty(); });
127 return take_front_();
133 std::lock_guard<std::mutex> lock(mutex_);
141 std::lock_guard<std::mutex> lock(mutex_);
147 std::lock_guard<std::mutex> lock(mutex_);
148 return queue_.size();
153 std::lock_guard<std::mutex> lock(mutex_);
154 return queue_.empty();
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