23 #ifndef LOCKED_QUEUE_HPP
24 #define LOCKED_QUEUE_HPP
26 #include <condition_variable>
45 std::lock_guard<std::timed_mutex> lock(mutex_);
46 return queue_.empty();
54 std::scoped_lock<std::timed_mutex> lock(mutex_);
57 condition_variable_.notify_one();
62 std::scoped_lock<std::timed_mutex> lock(mutex_);
65 condition_variable_.notify_one();
72 std::unique_lock<std::timed_mutex> lock(mutex_);
74 condition_variable_.wait(lock, [
this] {
return !this->queue_.empty(); });
76 auto item = queue_.front();
85 template <
typename Tick,
typename Period>
86 std::optional<T>
dequeue(
const std::chrono::duration<Tick, Period>& duration) {
87 std::unique_lock<std::timed_mutex> lock(mutex_, duration);
89 return std::optional<T>(std::nullopt);
92 if (!queue_.empty()) {
93 auto item = queue_.front();
96 return std::optional(item);
99 if (!condition_variable_.wait_for(lock, duration, [
this] { return !this->queue_.empty(); })) {
101 return std::optional<T>(std::nullopt);
104 auto item = queue_.front();
107 return std::optional(item);
113 template <
typename Tick,
typename Period>
114 std::queue<T>
BatchDeque(
const std::chrono::duration<Tick, Period>& duration) {
115 std::queue<T> output;
116 std::unique_lock<std::timed_mutex> lock(mutex_, duration);
121 if (queue_.empty()) {
123 if (condition_variable_.wait_for(lock, duration,
124 [
this] { return !this->queue_.empty(); })) {
138 std::queue<T> queue_;
139 mutable std::timed_mutex mutex_;
140 std::condition_variable_any condition_variable_;
145 #endif // LOCKED_QUEUE_HPP