3#include <condition_variable>
32 mutable std::mutex mtx_;
35 std::condition_variable not_empty_cv_;
39 std::condition_variable not_full_cv_;
58 std::unique_lock<std::mutex> lock(mtx_);
60 not_full_cv_.wait(lock, [
this] {
return queue_.size() < capacity_; });
62 queue_.push(std::move(
value));
63 not_empty_cv_.notify_one();
78 std::lock_guard<std::mutex> lock(mtx_);
79 if (capacity_ > 0 && queue_.size() >= capacity_)
return false;
80 queue_.push(std::move(
value));
81 not_empty_cv_.notify_one();
95 std::unique_lock<std::mutex> lock(mtx_);
96 not_empty_cv_.wait(lock, [
this] {
return !queue_.empty(); });
97 T
value = std::move(queue_.front());
99 not_full_cv_.notify_one();
114 std::lock_guard<std::mutex> lock(mtx_);
115 if (queue_.empty())
return std::nullopt;
116 T
value = std::move(queue_.front());
118 not_full_cv_.notify_one();
130 std::lock_guard<std::mutex> lock(mtx_);
131 return queue_.empty();
142 std::lock_guard<std::mutex> lock(mtx_);
143 return queue_.size();
194 template <
typename... Args>
196 std::unique_lock<std::mutex> lock(mtx_);
198 not_full_cv_.wait(lock, [
this] {
return queue_.size() < capacity_; });
200 queue_.emplace(std::forward<Args>(args)...);
201 not_empty_cv_.notify_one();
216 template <
typename... Args>
218 std::lock_guard<std::mutex> lock(mtx_);
219 if (capacity_ > 0 && queue_.size() >= capacity_) {
222 queue_.emplace(std::forward<Args>(args)...);
223 not_empty_cv_.notify_one();
254 out = std::move(*opt);
279 std::lock_guard<std::mutex> lock(mtx_);
280 if (queue_.empty()) {
283 out = queue_.front();
297 std::optional<T>
peek()
const {
298 std::lock_guard<std::mutex> lock(mtx_);
299 if (queue_.empty()) {
302 return queue_.front();
ThreadSafeQueue(size_t capacity=0)
Construct a queue with optional capacity.
Definition thread_safe_queue.h:43
T wait_dequeue()
Wait for and dequeue a value from the queue.
Definition thread_safe_queue.h:236
bool peek(T &out) const
Peek at the front item without removing it.
Definition thread_safe_queue.h:278
ThreadSafeQueue & operator=(const ThreadSafeQueue &)=delete
bool try_enqueue(T value)
Try to enqueue a value without blocking.
Definition thread_safe_queue.h:181
bool try_push(T value)
Try to push a value into the queue without blocking.
Definition thread_safe_queue.h:77
void push(T value)
Push a value into the queue.
Definition thread_safe_queue.h:57
bool try_emplace(Args &&... args)
Try to emplace a value into the queue in place without blocking.
Definition thread_safe_queue.h:217
bool empty() const
Check whether the queue is empty.
Definition thread_safe_queue.h:129
T wait_and_pop()
Wait for and pop a value from the queue.
Definition thread_safe_queue.h:94
void emplace(Args &&... args)
Emplace a value into the queue in place (blocking if bounded and full).
Definition thread_safe_queue.h:195
ThreadSafeQueue(const ThreadSafeQueue &)=delete
std::optional< T > peek() const
Peek at the front item without removing it.
Definition thread_safe_queue.h:297
std::optional< T > try_pop()
Try to pop a value from the queue without blocking.
Definition thread_safe_queue.h:113
bool try_dequeue(T &out)
Try to dequeue a value without blocking.
Definition thread_safe_queue.h:249
size_t size() const
Get the current number of items in the queue.
Definition thread_safe_queue.h:141
size_t capacity() const
Get the maximum capacity of the queue.
Definition thread_safe_queue.h:152
std::optional< T > try_dequeue()
Try to dequeue a value without blocking.
Definition thread_safe_queue.h:267
void enqueue(T value)
Enqueue a value into the queue (blocking if bounded and full).
Definition thread_safe_queue.h:168
uint8_t * value
Definition co_dictionary.h:9
Definition thread_safe_queue.h:8