26 #ifndef LBANN_UTILS_THREADS_THREAD_SAFE_QUEUE_HPP_INCLUDED 27 #define LBANN_UTILS_THREADS_THREAD_SAFE_QUEUE_HPP_INCLUDED 31 #include <condition_variable> 73 auto new_value = std::make_unique<T>(std::move(value));
74 auto new_node = std::make_unique<_Node>();
78 std::lock_guard<std::mutex> lk(
tail_mtx_);
90 std::lock_guard<std::mutex> lk(
head_mtx_);
106 std::unique_lock<std::mutex> lk(
head_mtx_);
111 auto popped_head = std::move(
head_);
112 head_ = std::move(popped_head->next_);
114 return std::move(popped_head->data_);
120 std::unique_lock<std::mutex> lk(
head_mtx_);
132 auto popped_head = std::move(
head_);
133 head_ = std::move(popped_head->next_);
135 return std::move(popped_head->data_);
141 std::lock_guard<std::mutex> lk(
head_mtx_);
149 std::lock_guard<std::mutex> lk(
tail_mtx_);
void set_stop_threads(bool flag)
Allow the thread pool to set / reset the flags.
std::unique_ptr< T > try_pop()
Try to remove the first value from the queue.
std::unique_ptr< _Node > head_
The first node in the list.
bool empty() const
Check if queue is empty.
std::unique_ptr< T > data_
std::unique_ptr< _Node > next_
std::unique_ptr< T > wait_and_pop()
Wait for data and then return it.
std::mutex tail_mtx_
The mutex protecting the tail of the list.
std::condition_variable data_available_
Condition variable tripped when data added.
std::mutex head_mtx_
The mutex protecting the head of the list.
void push(T value)
Adds a value to back of the queue.
thread_safe_queue()
Default constructor; creates an empty queue.
_Node * do_get_tail_() const
Get the tail pointer.
_Node * tail_
The last node in the list.
A queue that is safe for multiple threads to push to or pull from "simultaneously".
A data value in the thread-safe FIFO queue.
void wake_all(bool stop=false)