4#include <condition_variable>
20 std::condition_variable cv_;
21 mutable std::mutex mutex_;
28 [[nodiscard]]
auto size() const noexcept {
29 auto const lock = std::lock_guard(mutex_);
37 [[nodiscard]]
auto empty() const noexcept {
38 auto const lock = std::lock_guard(mutex_);
39 return queue_.empty();
46 void push(T value)
noexcept {
47 auto const lock = std::lock_guard(mutex_);
48 queue_.push(std::move(value));
56 auto const lock = std::lock_guard(mutex_);
60 decltype(queue_)().swap(queue_);
68 [[nodiscard]]
auto pop(std::chrono::nanoseconds wait_time = {}) -> std::optional<T> {
69 auto lock = std::unique_lock(mutex_);
72 if (!cv_.wait_for(lock, wait_time, [
this] { return !queue_.empty(); }))
return std::nullopt;
74 auto value = queue_.front();
Thread-safe queue. Particularly useful when multiple threads need to write to and/or read from a queu...
Definition queue.hpp:18
void clear() noexcept
Clear the queue.
Definition queue.hpp:55
auto size() const noexcept
Get the size of the queue.
Definition queue.hpp:28
void push(T value) noexcept
Push data into the queue.
Definition queue.hpp:46
auto empty() const noexcept
Check if the queue is empty.
Definition queue.hpp:37
auto pop(std::chrono::nanoseconds wait_time={}) -> std::optional< T >
Wait for given duration then pop from the queue and return the element.
Definition queue.hpp:68