A header-only style C++ queue for one producer and one consumer thread without locks. Includes a optional blocking circular-buffer variant.
Overview
This package provides a lock-free queue for C++ aimed at single-producer, single-consumer workloads. One thread enqueues and another dequeues; roles do not switch, though a single thread could use both sides. The author notes a separate multi-producer, multi-consumer queue for broader parallelism.
The main queue is templated for any element type, pre-allocates contiguous blocks, and supports C++11 move semantics. try_enqueue never allocates after the initial capacity; enqueue can grow the queue. try_emplace and emplace shortcuts are included. Operations are wait-free with O(1) enqueue and dequeue aside from growth allocation, and on x86 memory barriers may compile to no-ops.
A circular-buffer variant adds blocking wait_dequeue and timed wait with configurable spin counts before OS blocking. Integration is by including readerwriterqueue.h, readerwritercircularbuffer.h, and atomicops.h; CMake targets can vend the headers system-wide.
Documentation warns the queue suits platforms where aligned integer and pointer access is atomic, with primary testing on x86 and x86-64. GCC 4.7 or newer is required for correct atomic fence behavior.
Key features
- Lock-free SPSC queue
- Blocking circular buffer variant
- try_enqueue without allocation
- C++11 move-friendly templates
- wait_dequeue blocking API
- CMake install and target support
- Pre-allocated contiguous storage
Use cases
- Audio or sensor producer-consumer pipelines
- Game engine job handoff between threads
- Logging or metrics off hot paths
- Embedded-style two-thread message passing