ros2_control - kilted
Loading...
Searching...
No Matches
Public Member Functions | List of all members
realtime_tools::LockFreePooledSPSCQueue< T, Capacity > Class Template Reference

Wait-free SPSC queue for large or move-only types, backed by a preallocated pool. More...

#include <lock_free_pooled_queue.hpp>

Public Member Functions

 LockFreePooledSPSCQueue ()
 Construct the queue with all slots free.
 
template<typename Fill >
bool produce (Fill &&fill)
 Fill a free slot in place and publish it to the consumer.
 
template<typename U = T, std::enable_if_t< std::is_same_v< U, T > &&std::is_nothrow_move_assignable_v< U >, int > = 0>
bool push (T &&value)
 Move the value into a free slot and publish it to the consumer.
 
template<typename Use >
bool consume (Use &&use)
 Read the oldest published slot in place and release it back to the pool.
 
template<typename U = T, std::enable_if_t< std::is_same_v< U, T > &&std::is_nothrow_move_assignable_v< U >, int > = 0>
bool pop (T &out)
 Move the oldest element out of the queue.
 
bool empty () const
 

Detailed Description

template<typename T, std::size_t Capacity>
class realtime_tools::LockFreePooledSPSCQueue< T, Capacity >

Wait-free SPSC queue for large or move-only types, backed by a preallocated pool.

The elements live in a fixed pool of Capacity slots. Only slot indices travel through two LockFreeSPSCQueues: one with the filled slots (producer -> consumer) and one with the free slots (consumer -> producer). Elements are therefore never copied by the queue, can be move-only and are written/read in place with produce()/consume().

Template Parameters
TElement type. Must be default constructible (all slots are constructed up front).
CapacityNumber of slots in the pool, must be > 0.
Note
Exactly one producer thread (push/produce) and exactly one consumer thread (pop/consume). empty() may only be called from the consumer thread.
The queue holds Capacity * sizeof(T) bytes inline, so allocate it on the heap or as a member if it is large.

Member Function Documentation

◆ consume()

template<typename T , std::size_t Capacity>
template<typename Use >
bool realtime_tools::LockFreePooledSPSCQueue< T, Capacity >::consume ( Use &&  use)
inline

Read the oldest published slot in place and release it back to the pool.

Parameters
useCallable invoked as use(T &) with the slot to read. It must not throw.
Returns
true If a slot was consumed, false if the queue is empty (nothing is called).
Note
Consumer thread only.

◆ empty()

template<typename T , std::size_t Capacity>
bool realtime_tools::LockFreePooledSPSCQueue< T, Capacity >::empty ( ) const
inline
Returns
true if there is nothing to consume. Consumer thread only.

◆ pop()

template<typename T , std::size_t Capacity>
template<typename U = T, std::enable_if_t< std::is_same_v< U, T > &&std::is_nothrow_move_assignable_v< U >, int > = 0>
bool realtime_tools::LockFreePooledSPSCQueue< T, Capacity >::pop ( T &  out)
inline

Move the oldest element out of the queue.

Returns
true If an element was popped, false if the queue is empty.
Note
Only available if T is nothrow move assignable, a throwing assignment would lose the slot from the pool.
Consumer thread only.

◆ produce()

template<typename T , std::size_t Capacity>
template<typename Fill >
bool realtime_tools::LockFreePooledSPSCQueue< T, Capacity >::produce ( Fill &&  fill)
inline

Fill a free slot in place and publish it to the consumer.

Parameters
fillCallable invoked as fill(T &) with the slot to write. It must not throw: the slot would be lost from the pool otherwise.
Returns
true If the slot was published, false if all slots are in use (nothing is called).
Note
Producer thread only.

◆ push()

template<typename T , std::size_t Capacity>
template<typename U = T, std::enable_if_t< std::is_same_v< U, T > &&std::is_nothrow_move_assignable_v< U >, int > = 0>
bool realtime_tools::LockFreePooledSPSCQueue< T, Capacity >::push ( T &&  value)
inline

Move the value into a free slot and publish it to the consumer.

Returns
true If pushed, false if all slots are in use.
Note
Only available if T is nothrow move assignable, a throwing assignment would lose the slot from the pool.
Producer thread only. Uses T's move assignment into the preallocated slot, so use produce() instead if that assignment can allocate or deallocate.

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