Template Class LockFreeQueueBase

Class Documentation

template<typename DataType, typename LockFreeContainer>
class LockFreeQueueBase

A base class for lock-free queues.

This class provides a base implementation for lock-free queues with various functionalities such as pushing, popping, and checking the state of the queue. It supports both single-producer single-consumer (SPSC) and multiple-producer multiple-consumer (MPMC) queues.

Template Parameters:
  • DataType – The type of data to be stored in the queue.

  • LockFreeContainer – The underlying lock-free container type - Typically boost::lockfree::spsc_queue or boost::lockfree::queue with their own template parameters

Public Types

using T = DataType

Public Functions

template<bool HasCapacity = has_capacity<LockFreeContainer>::value, typename std::enable_if_t<HasCapacity, int> = 0>
inline LockFreeQueueBase()

Construct a new LockFreeQueueBase object.

Note

This constructor is enabled only if the LockFreeContainer has a capacity set

template<bool HasCapacity = has_capacity<LockFreeContainer>::value, typename std::enable_if_t<!HasCapacity, int> = 1>
inline explicit LockFreeQueueBase(std::size_t capacity)

Construct a new LockFreeQueueBase object.

Note

This constructor is enabled only if the LockFreeContainer has no capacity set

Parameters:

capacity – Capacity of the queue

virtual ~LockFreeQueueBase() = default
inline bool pop(T &data)

Pop the data from the queue.

Parameters:

data – Data to be popped

Returns:

true If the data is popped successfully

Returns:

false If the queue is empty or the data could not be popped

template<typename U>
inline std::enable_if_t<std::is_convertible_v<T, U>, bool> pop(U &data)

Pop the data from the queue.

Note

This function is enabled only if the data type is convertible to the template type of the queue

Parameters:

data – Data to be popped

Returns:

true If the data is popped successfully

Returns:

false If the queue is empty or the data could not be popped

inline bool push(const T &data)

Push the data into the queue.

Parameters:

data – Data to be pushed

Returns:

true If the data is pushed successfully

Returns:

false If the queue is full or the data could not be pushed

template<typename U>
inline std::enable_if_t<std::is_convertible_v<T, U>, bool> push(const U &data)

Push the data into the queue.

Note

This function is enabled only if the data type is convertible to the template type of the queue

Parameters:

data – Data to be pushed

Returns:

true If the data is pushed successfully

Returns:

false If the queue is full or the data could not be pushed

template<typename U>
inline std::enable_if_t<std::is_convertible_v<T, U>, bool> bounded_push(const U &data)

The bounded_push function pushes the data into the queue and pops the oldest data if the queue is full.

Note

This function is enabled only if the queue is a spsc_queue and only if the data type is convertible to the template type of the queue

Note

For a SPSC Queue, to be used in single-threaded applications

Warning

For a SPSC Queue, this method might not work as expected when used in multi-threaded applications if it is used with two different threads, one doing bounded_push and the other doing pop. In this case, the queue is no longer a single producer single consumer queue. So, the behavior might not be as expected.

Parameters:

data – Data to be pushed

Returns:

true If the data is pushed successfully

Returns:

false If the data could not be pushed

inline bool get_latest(T &data)

Get the latest data from the queue.

Note

This function consumes all the data in the queue until the last data and returns the last element of the queue

Parameters:

data – Data to be filled with the latest data

Returns:

true If the data is filled with the latest data, false otherwise

Returns:

false If the queue is empty

inline bool empty() const

Check if the queue is empty.

Note

The result is only accurate, if no other thread modifies the queue. Therefore, it is rarely practical to use this value in program logic.

Note

For a SPSC Queue, it should only be called from the consumer thread where pop is called. If need to be called from producer thread, use write_available() instead.

Returns:

true If the queue is empty

Returns:

false If the queue is not empty

inline size_t capacity() const

Get the capacity of the queue.

Returns:

std::size_t Capacity of the queue

template<bool IsSPSCQueue = is_spsc_queue<LockFreeContainer>::value, typename std::enable_if_t<IsSPSCQueue, int> = 0>
inline std::size_t size() const

Get the size of the queue.

Note

This function is enabled only if the queue is a spsc_queue

Returns:

std::size_t Size of the queue

inline bool is_lock_free() const

The method to check if the queue is lock free.

Warning

It only checks, if the queue head and tail nodes and the freelist can be modified in a lock-free manner. On most platforms, the whole implementation is lock-free, if this is true. Using c++0x-style atomics, there is no possibility to provide a completely accurate implementation, because one would need to test every internal node, which is impossible if further nodes will be allocated from the operating system. https://www.boost.org/doc/libs/1_74_0/doc/html/boost/lockfree/queue.html

Returns:

true If the queue is lock free, false otherwise

inline const LockFreeContainer &get_lockfree_container() const

Get the lockfree container.

Returns:

const LockFreeContainer& Reference to the lockfree container

inline LockFreeContainer &get_lockfree_container()

Get the lockfree container.

Returns:

LockFreeContainer& Reference to the lockfree container