Designing lock-free SPSC queue with peek and cache line alignment
Proposed a single-producer single-consumer circular queue in C using C11 atomics with head and tail indices aligned to separate cache lines via padding to prevent false sharing. Included enqueue, dequeue, and peek operations using acquire-release memory ordering to ensure safe concurrency, with peek returning the next item without advancing the tail.
For a single‑producer/single‑consumer (SPSC) sensor stream you can keep it very lean: one writer advances the head, one reader advances the tail. Use C11 atomics for the indices and arrange them so they don’t share a cache line.
The example below assumes a power‑of‑two capacity so that modulo can be done with a bitmask; you can drop that if you prefer a general modulus.
#include <stdalign.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
enum { CACHELINE_SIZE = 64 };
typedef struct {
uint64_t timestamp;
float value;
/* whatever your sensor payload is */
} sensor_sample_t;
typedef struct {
alignas(CACHELINE_SIZE) atomic_size_t head;
/* pad out to a full cache line so tail lives elsewhere */
char _pad1[CACHELINE_SIZE - sizeof(atomic_size_t)];
alignas(CACHELINE_SIZE) atomic_size_t tail;
char _pad2[CACHELINE_SIZE - sizeof(atomic_size_t)];
size_t size_mask; /* capacity‑1 if power of two */
sensor_sample_t *buf;
} spsc_queue_t;
static inline bool
spsc_init(spsc_queue_t *q, size_t capacity, sensor_sample_t *storage)
{
/* capacity must be a power of two for the mask trick */
if ((capacity == 0) || (capacity & (capacity - 1U)) != 0)
return false;
atomic_init(&q->head, 0);
atomic_init(&q->tail, 0);
q->size_mask = capacity - 1U;
q->buf = storage;
return true;
}
static inline bool
spsc_enqueue(spsc_queue_t *q, const sensor_sample_t *item)
{
size_t head = atomic_load_explicit(&q->head, memory_order_relaxed);
size_t next = (head + 1U) & q->size_mask;
/* acquire tail to see the consumer's progress */
size_t tail = atomic_load_explicit(&q->tail, memory_order_acquire);
if (next == tail) {
/* queue full */
return false;
}
q->buf[head] = *item;
/* publish the new head */
atomic_store_explicit(&q->head, next, memory_order_release);
return true;
}
static inline bool
spsc_dequeue(spsc_queue_t *q, sensor_sample_t *out)
{
size_t tail = atomic_load_explicit(&q->tail, memory_order_relaxed);
/* acquire head to see the producer's progress */
size_t head = atomic_load_explicit(&q->head, memory_order_acquire);
if (tail == head) {
/* queue empty */
return false;
}
*out = q->buf[tail];
/* release the slot back to the producer */
atomic_store_explicit(&q->tail, (tail + 1U) & q->size_mask,
memory_order_release);
return true;
}
static inline bool
spsc_peek(const spsc_queue_t *q, sensor_sample_t *out)
{
size_t tail = atomic_load_explicit(&q->tail, memory_order_relaxed);
size_t head = atomic_load_explicit(&q->head, memory_order_acquire);
if (tail == head) {
return false; /* empty */
}
*out = q->buf[tail];
/* note: tail is not advanced */
return true;
}
You provide the backing storage when initializing:
#define QUEUE_CAP 1024
static sensor_sample_t storage[QUEUE_CAP];
static spsc_queue_t queue;
int main(void) {
if (!spsc_init(&queue, QUEUE_CAP, storage)) { /* handle error */ }
/* producer thread calls spsc_enqueue, consumer calls spsc_dequeue/peek */
}
By aligning head and tail to separate 64‑byte boundaries and padding between them, updates from producer and consumer won’t thrash the same cache line. The use of memory_order_release on the producer’s head update and memory_order_acquire on the consumer’s head load ensures the sample payload is visible to the consumer; similarly, the tail update uses memory_order_release so the producer can see freed slots.