LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
lbann::thread_safe_queue< T > Class Template Reference

A queue that is safe for multiple threads to push to or pull from "simultaneously". More...

#include <thread_safe_queue.hpp>

Collaboration diagram for lbann::thread_safe_queue< T >:
[legend]

Classes

class  _Node
 A data value in the thread-safe FIFO queue. More...
 

Public Member Functions

 thread_safe_queue ()
 Default constructor; creates an empty queue. More...
 
void push (T value)
 Adds a value to back of the queue. More...
 
void wake_all (bool stop=false)
 
void set_stop_threads (bool flag)
 Allow the thread pool to set / reset the flags. More...
 
std::unique_ptr< T > try_pop ()
 Try to remove the first value from the queue. More...
 
std::unique_ptr< T > wait_and_pop ()
 Wait for data and then return it. More...
 
bool empty () const
 Check if queue is empty. More...
 

Private Member Functions

_Nodedo_get_tail_ () const
 Get the tail pointer. More...
 

Private Attributes

std::mutex head_mtx_
 The mutex protecting the head of the list. More...
 
std::mutex tail_mtx_
 The mutex protecting the tail of the list. More...
 
std::unique_ptr< _Nodehead_
 The first node in the list. More...
 
_Nodetail_
 The last node in the list. More...
 
std::condition_variable data_available_
 Condition variable tripped when data added. More...
 
bool m_stop_threads
 

Detailed Description

template<typename T>
class lbann::thread_safe_queue< T >

A queue that is safe for multiple threads to push to or pull from "simultaneously".

This version uses locks.

This is essentially a fancy linked-list implementation that enables finer-grained locks than simply wrapping an std::queue. The trade-off is two locks, one for the front and one for the back of the list.

Template Parameters
TA move- or copy-constructible type

Definition at line 51 of file thread_safe_queue.hpp.

Constructor & Destructor Documentation

◆ thread_safe_queue()

template<typename T>
lbann::thread_safe_queue< T >::thread_safe_queue ( )
inline

Default constructor; creates an empty queue.

Definition at line 65 of file thread_safe_queue.hpp.

Member Function Documentation

◆ do_get_tail_()

template<typename T>
_Node* lbann::thread_safe_queue< T >::do_get_tail_ ( ) const
inlineprivate

Get the tail pointer.

Definition at line 147 of file thread_safe_queue.hpp.

Here is the caller graph for this function:

◆ empty()

template<typename T>
bool lbann::thread_safe_queue< T >::empty ( ) const
inline

Check if queue is empty.

Definition at line 139 of file thread_safe_queue.hpp.

◆ push()

template<typename T>
void lbann::thread_safe_queue< T >::push ( value)
inline

Adds a value to back of the queue.

Definition at line 70 of file thread_safe_queue.hpp.

◆ set_stop_threads()

template<typename T>
void lbann::thread_safe_queue< T >::set_stop_threads ( bool  flag)
inline

Allow the thread pool to set / reset the flags.

Definition at line 98 of file thread_safe_queue.hpp.

◆ try_pop()

template<typename T>
std::unique_ptr<T> lbann::thread_safe_queue< T >::try_pop ( )
inline

Try to remove the first value from the queue.

Returns
nullptr if empty(); otherwise return a value

Definition at line 104 of file thread_safe_queue.hpp.

◆ wait_and_pop()

template<typename T>
std::unique_ptr<T> lbann::thread_safe_queue< T >::wait_and_pop ( )
inline

Wait for data and then return it.

Definition at line 118 of file thread_safe_queue.hpp.

◆ wake_all()

template<typename T>
void lbann::thread_safe_queue< T >::wake_all ( bool  stop = false)
inline

Definition at line 87 of file thread_safe_queue.hpp.

Member Data Documentation

◆ data_available_

template<typename T>
std::condition_variable lbann::thread_safe_queue< T >::data_available_
private

Condition variable tripped when data added.

Definition at line 167 of file thread_safe_queue.hpp.

◆ head_

template<typename T>
std::unique_ptr<_Node> lbann::thread_safe_queue< T >::head_
private

The first node in the list.

Definition at line 161 of file thread_safe_queue.hpp.

◆ head_mtx_

template<typename T>
std::mutex lbann::thread_safe_queue< T >::head_mtx_
mutableprivate

The mutex protecting the head of the list.

Definition at line 155 of file thread_safe_queue.hpp.

◆ m_stop_threads

template<typename T>
bool lbann::thread_safe_queue< T >::m_stop_threads
private

Definition at line 169 of file thread_safe_queue.hpp.

◆ tail_

template<typename T>
_Node* lbann::thread_safe_queue< T >::tail_
private

The last node in the list.

Definition at line 164 of file thread_safe_queue.hpp.

◆ tail_mtx_

template<typename T>
std::mutex lbann::thread_safe_queue< T >::tail_mtx_
mutableprivate

The mutex protecting the tail of the list.

Definition at line 158 of file thread_safe_queue.hpp.


The documentation for this class was generated from the following file: