10#include <condition_variable>
41 std::lock_guard<std::mutex> lock(
_mutex);
42 _queue.push(std::move(item));
53 std::lock_guard<std::mutex> lock(
_mutex);
57 T item = std::move(
_queue.front());
69 std::unique_lock<std::mutex> lock(
_mutex);
70 _cv.wait(lock, [
this] {
return !
_queue.empty(); });
71 T item = std::move(
_queue.front());
83 std::lock_guard<std::mutex> lock(
_mutex);
94 std::lock_guard<std::mutex> lock(
_mutex);
104 std::lock_guard<std::mutex> lock(
_mutex);
105 std::queue<T> emptyQueue;
106 std::swap(
_queue, emptyQueue);
111 std::condition_variable
_cv;
Thread-safe queue for inter-thread communication.
T pop()
Pop an item, blocking until one is available.
void push(T item)
Push an item to the queue.
bool empty() const
Check if queue is empty.
ThreadSafeQueue & operator=(const ThreadSafeQueue &)=delete
~ThreadSafeQueue()=default
ThreadSafeQueue(const ThreadSafeQueue &)=delete
std::optional< T > tryPop()
Try to pop an item without blocking.
size_t size() const
Get queue size.
void clear()
Clear all items from queue.
std::condition_variable _cv
ThreadSafeQueue()=default