Wait-free SPSC queue for large or move-only types, backed by a preallocated pool.
More...
|
|
| 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 |
| |
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
-
| T | Element type. Must be default constructible (all slots are constructed up front). |
| Capacity | Number 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.
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>
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.
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>
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.