SPSCQueue¶
SPSCQueue is a lock-free, bounded-size single-producer/single-consumer queue optimized for HFT workloads. It supports in-place construction, zero allocations, and cache-line isolation.
Purpose¶
- Provide low-latency, zero-contention messaging between one writer and one reader.
Requirements¶
Capacitymust be a power of two.Tmust be nothrow-destructible.- Only one producer and one consumer may operate concurrently.
Key Features¶
| Method | Description |
|---|---|
push(const T&) |
Enqueues a copy of an object. |
emplace(T&&) |
Enqueues an rvalue object (move). |
try_emplace(...) |
Constructs object in-place with arguments. |
pop(T&) |
Pops and moves the front element into out. |
try_pop() |
Returns a pointer to the front element, or nullptr if empty. |
try_pop_ref() |
Returns std::optional<std::reference_wrapper<T>> for inline access. |
empty() / full() |
Check queue state. |
clear() |
Destroys and drains all pending elements. |
size() |
Returns current number of elements. |
Internal Design¶
- Ring buffer implementation with
Capacityentries, using moduloMASK = Capacity - 1. _headand_tailarestd::atomic<size_t>and are false-shared-safe viaalignas(64).- Uses placement
newfor in-place construction, avoids heap entirely.
Notes¶
- Optimized for predictable, sub-microsecond latency in tight loops.
- No memory reclamation or ABA protection — not suitable for multi-producer/multi-consumer setups.
- All methods use
memory_order_acquire/releaseto ensure visibility across cores. - Destruction ensures safe draining of remaining elements via
~T()call.