17#include <teiacare/sdk/non_copyable.hpp>
18#include <teiacare/sdk/non_moveable.hpp>
21#include <condition_variable>
67 std::unique_lock
lock(_mutex);
69 _last_item_popped.wait(
lock, [
this] {
return !is_full(); });
72 push_impl(std::move(
lock));
84 std::unique_lock
lock(_mutex);
86 _last_item_popped.wait(
lock, [
this] {
return !is_full(); });
89 push_impl(std::move(
lock));
102 std::unique_lock
lock(_mutex);
107 push_impl(std::move(
lock));
123 std::unique_lock
lock(_mutex);
127 _queue.emplace(
item);
128 push_impl(std::move(
lock));
141 std::unique_lock
lock(_mutex);
143 _first_item_pushed.wait(
lock, [
this] {
return !is_empty(); });
145 T
item = _queue.front();
147 pop_impl(std::move(
lock));
161 std::unique_lock
lock(_mutex);
165 T
item = _queue.front();
167 pop_impl(std::move(
lock));
169 return std::move(std::optional(
item));
178 std::lock_guard
lock(_mutex);
179 return _queue.size();
188 std::lock_guard
lock(_mutex);
193 std::queue<T> _queue;
194 mutable std::mutex _mutex;
195 std::condition_variable _last_item_popped;
196 std::condition_variable _first_item_pushed;
197 const size_t _capacity;
199 inline void push_impl(std::unique_lock<std::mutex>&&
lock)
205 _first_item_pushed.notify_all();
208 inline void pop_impl(std::unique_lock<std::mutex>&& lock)
210 const bool is_last_item_popped = _queue.
size() == _capacity - 1;
213 if (is_last_item_popped)
214 _last_item_popped.notify_all();
217 inline bool is_empty()
const
219 return _queue.empty();
221 inline bool is_full()
const
223 return _queue.
size() >= _capacity;
Thread safe, blocking queue.
std::optional< T > try_pop()
Try to retrieve an item from the queue.
bool try_push(T &&item)
Try to insert an item into the queue.
blocking_queue(size_t capacity)
Constructor.
size_t capacity() const
Get the maximum number of items that the queue can hold.
void push(T &&item)
Insert an item into the queue.
T pop()
Retrieve an item from the queue.
bool try_push(const T &item)
Try to insert an item into the queue.
void push(const T &item)
Insert an item into the queue.
~blocking_queue()=default
Destructor.
size_t size() const
Get the number of items currently in the queue.
Utility class expected to be used as a base class to define non-copyable classes.
Utility class expected to be used as a base class to define non-moveable classes.