$ show cameron314/readerwriterqueue
This repository provides a high-performance, header-only C++11 library implementing a lock-free, wait-free single-producer single-consumer (SPSC) queue (ReaderWriterQueue) and a blocking variant (BlockingReaderWriterQueue). The core design uses a queue-of-blocks architecture with circular buffers to minimize memory allocation and contention. It also includes a fixed-size blocking circular buffer (BlockingReaderWriterCircularBuffer) with support for timed waits. The library is built on top of a portable atomic operations layer (atomicops.h) that abstracts memory barriers, compiler fences, and a lightweight semaphore across Windows, macOS, Linux, and FreeRTOS. The code demonstrates exceptional attention to cache-line padding, memory ordering, and elimination of false sharing, making it suitable for ultra-low-latency inter-thread communication. It is extensively tested with unit tests and stability tests, and includes benchmarks comparing performance against other SPSC queues such as Dmitry Vyukov's and Facebook's folly. The library is single-purpose, self-contained, and easily integrable into any C++ project via drop-in headers or CMake FetchContent.
The LLM identified 3 files worth highlighting for reuse:
Implements the core lock-free SPSC queue (ReaderWriterQueue) and its blocking wrapper (BlockingReaderWriterQueue). The queue uses a linked list of fixed-size circular buffer blocks to allow dynamic growth with amortized O(1) enqueue and dequeue. Enqueue operations are wait-free in the common case; only memory allocation may block. The implementation carefully places hot fields on separate cache lines to avoid false sharing and uses portable memory fences for correct ordering. This component is the primary deliverable of the repository and is reusable in any C++11 project requiring a high-performance SPSC queue.
Provides a portable abstraction layer for low-level atomic operations, memory ordering fences, and compiler barriers across MSVC, GCC, ICC, and C++11 standard atomics. Includes a custom weak_atomic type for types with naturally atomic load/store (e.g., aligned pointers and integers) without memory ordering guarantees. Also supplies a lightweight semaphore (LightweightSemaphore) adapted from Jeff Preshing's implementation, supporting Windows, macOS, POSIX, and FreeRTOS. This header is essential for building lock-free data structures without platform-specific code and is reusable as a standalone concurrency primitive library.
Implements a fixed-capacity, blocking SPSC circular buffer (BlockingReaderWriterCircularBuffer). Uses two lightweight semaphores to coordinate producer and consumer threads, supporting blocking wait_enqueue/wait_dequeue with optional timeouts. The buffer capacity is rounded up to a power of two for efficient modulo operations. Element storage is dynamically allocated and aligned. This component is useful for scenarios requiring a bounded queue with backpressure and blocking semantics, such as streaming or pipeline architectures.