Skip to main content

Module queue

Module queue 

Source
Expand description

Lock-free MPMC bounded queue using a ring buffer with atomic operations.

This module implements a high-performance multi-producer, multi-consumer bounded queue that avoids mutexes on the hot path. Each slot has its own sequence number so producers and consumers can independently claim a slot without blocking one another.

§Algorithm

Each ring-buffer slot stores:

  • sequence: AtomicUsize — an ever-increasing stamp that encodes the slot state (empty/ready-to-read).
  • value: UnsafeCell<MaybeUninit<T>> — the payload.

A producer:

  1. Atomically increments the shared tail.
  2. Waits (spin) until slot.sequence == tail (the slot was last read by tail - capacity ago, so it is now free).
  3. Writes the value and sets slot.sequence = tail + 1 (signals the consumer that the slot is ready).

A consumer mirrors the process using head.

This is the classic Dmitry Vyukov MPMC queue design.

Structs§

LockFreeQueue
A bounded, lock-free multi-producer / multi-consumer queue.