LibraryLink Utilities  3.0.1
Modern C++ wrapper over LibraryLink and WSTP
LLU::Async::ThreadsafeQueue< T > Class Template Reference

Description

template<typename T>
class LLU::Async::ThreadsafeQueue< T >

ThreadsafeQueue is a linked list of nodes which supports safe concurrent access to its head (removing elements) and tail (adding new elements).

ThreadsafeQueue is described in chapter 6 of A. Williams "C++ Concurrency in Action" 2nd Edition. This implementation contains only slight modifications and minor bugfixes.

Template Parameters
T- type of the data stored in the Queue

#include <Queue.h>

Public Types

using value_type = T
 Value type of queue elements. More...
 

Public Member Functions

 ThreadsafeQueue ()
 Create new empty queue. More...
 
std::shared_ptr< value_typetryPop ()
 Get data from the queue if available. More...
 
bool tryPop (value_type &value)
 Get data from the queue if available. More...
 
std::shared_ptr< value_typewaitPop ()
 Get data from the queue, possibly waiting for it. More...
 
void waitPop (value_type &value)
 Get data from the queue, possibly waiting for it. More...
 
void push (value_type new_value)
 Push new value to the end of the queue. More...
 
bool empty () const
 Check if the queue is empty. More...
 

Type aliases documentation

◆ value_type

template<typename T >
using LLU::Async::ThreadsafeQueue< T >::value_type = T

Value type of queue elements.

Constructor & Destructor Documentation

◆ ThreadsafeQueue()

template<typename T >
LLU::Async::ThreadsafeQueue< T >::ThreadsafeQueue ( )
inline

Create new empty queue.

Member Function Documentation

◆ empty()

template<typename T >
bool LLU::Async::ThreadsafeQueue< T >::empty

Check if the queue is empty.

Returns
True iff the queue is empty i.e. has no data to be popped.

◆ push()

template<typename T >
void LLU::Async::ThreadsafeQueue< T >::push ( value_type  new_value)

Push new value to the end of the queue.

This operation can be performed even with other thread popping a value from the queue at the same time.

Parameters
new_value- value to be pushed to the queue

◆ tryPop() [1/2]

template<typename T >
std::shared_ptr< T > LLU::Async::ThreadsafeQueue< T >::tryPop

Get data from the queue if available.

If data is not available in the queue, the calling thread will not wait.

Returns
Shared pointer to the data from the queue's head, nullptr if there is no data to be popped.

◆ tryPop() [2/2]

template<typename T >
bool LLU::Async::ThreadsafeQueue< T >::tryPop ( value_type value)

Get data from the queue if available.

If data is not available in the queue, the calling thread will not wait.

Parameters
[out]value- reference to the data from the queue
Returns
True iff there was data in the queue, otherwise the out-parameter remains unchanged.

◆ waitPop() [1/2]

template<typename T >
std::shared_ptr< T > LLU::Async::ThreadsafeQueue< T >::waitPop

Get data from the queue, possibly waiting for it.

Returns
Shared pointer to the data from the queue's head

◆ waitPop() [2/2]

template<typename T >
void LLU::Async::ThreadsafeQueue< T >::waitPop ( value_type value)

Get data from the queue, possibly waiting for it.

Parameters
value- reference to the data from the queue