Skip to main content

Module buffer

Module buffer 

Source
Expand description

Lock-free SPSC ring buffer.

The core data structure: a fixed-capacity circular buffer with cache-line-padded head (consumer) and tail (producer) counters. Only two atomic operations per enqueue/dequeue — one Relaxed load of the remote counter, one Release/Acquire store of the local counter.

§Memory layout

┌─────────────────────────────────────────────────────┐
│  [CacheLine] tail (written by producer only)        │
│  [CacheLine] head (written by consumer only)        │
│  [capacity]  slot array (T values)                  │
└─────────────────────────────────────────────────────┘

The tail and head live on separate cache lines to prevent false sharing. Capacity must be a power of two so we can use bitwise AND instead of modulo.

Structs§

Consumer
The consumer half of the ring buffer.
Producer
The producer half of the ring buffer.
RingBuffer
Create a new SPSC ring buffer with the given capacity.