Lock-free data structures
cmake -S . -B build # normal build
cmake -S . -B build-tsan -DLFDS_SANITIZER=thread # data race errors
cmake -S . -B build-asan -DLFDS_SANITIZER=address # memory errors
cmake --build build
ctest --test-dir build --output-on-failure
Tests are in tests/test_*.cpp
SPSC capacity is a power of 2
When capacity c = 2^k, pos % c equals pos & (c - 1). With a runtime capacity, % compiles to divide instruction ~20-40+ cycles, while the & takes 1 cycle. The positions are unsigned n bit integers that wrap from 2^n back to 0. The slot mapping stays continuous across the wrap only if 2^n mod c = 0, meaning c divides 2^n The only divisors of 2^n are powers of two, so c must be a power of two.
explicit SpscRingBuffer<T, Allocator = std::allocator<T>>(usize capacity, const Allocator& a = Allocator());
Allocates storage for capacity items. Throws std::invalid_argument if capacity is not a power of two.
Requires std::is_nothrow_destructible::value == true. Not copyable or movable.
void emplace(Args&&... args);
Enqueue an item using inplace construction. Blocks if queue is full.
bool try_emplace(Args&&... args);
Try to enqueue an item using inplace construction. Returns true on success and false if queue is full.
void push(const T& v);
Enqueue an item using copy construction. Blocks if queue is full.
void push(T&& v);
Enqueue an item using move construction. Blocks if queue is full.
bool try_push(const T& v);
Try to enqueue an item using copy construction. Returns true on success and false if queue is full.
bool try_push(T&& v);
Try to enqueue an item using move construction. Returns true on success and false if queue is full.
void pop(T& out);
Dequeue first item of queue by moving it into out. Blocks if queue is empty.
Requires std::is_move_assignable::value == true.
void pop();
Dequeue and discard first item of queue. Blocks if queue is empty.
bool try_pop(T& out);
Try to dequeue first item of queue by moving it into out. Returns true on success and false if queue is empty.
Requires std::is_move_assignable::value == true.
bool try_pop();
Try to dequeue and discard first item of queue. Returns true on success and false if queue is empty.
usize size() const;
Return the number of items available in the queue.
bool empty() const;
Return true if queue is currently empty.
bool full() const;
Return true if queue is currently full.
usize capacity() const;
Return the capacity of the queue.
Only a single writer thread can perform enqueue operations and only a single reader thread can perform dequeue operations. Any other usage is invalid.
bench/spsc_bench.cpp is built twice, as spsc_bench_64 and spsc_bench_128, differing only in LFDS_CACHE_LINE, the padding of the producer and consumer positions. The runner raises QoS to USER_INTERACTIVE on every thread, checks the machine with scripts/bench_env.sh, interleaves single runs of the two paddings, and records everything under results/.
cmake -S . -B build-bench -DCMAKE_BUILD_TYPE=Release -DLFDS_BUILD_TESTS=OFF \
-DCMAKE_CXX_FLAGS_RELEASE="-O2 -fno-omit-frame-pointer -march=native -DNDEBUG"
cmake --build build-bench
BUILD=build-bench ROUNDS=7 ./scripts/bench_spsc.sh # try_push / try_pop spin
BUILD=build-bench ROUNDS=7 ./scripts/bench_spsc.sh --blocking # push / pop, atomic wait and notify
stream: one thread pushes 100M ints through a capacity 1024 queue, the other pops and checksums. Reports M msgs/s. pingpong: two capacity 64 queues, A sends, B echoes, A times blocks of 64 round trips. Reports ns per round trip, p50 and p99.
Measured 2026-10-06 on an Apple M4 Max (128 B lines), GCC 16.2, 7 interleaved rounds, load average 4 to 5. Medians across rounds, spread is (max - min) / median.
| handoff | padding | stream M msgs/s | p50 ns | p99 ns |
|---|---|---|---|---|
| spin | 64 B | 52.3 (spread 10%) | 146.5 (9%) | 203 |
| spin | 128 B | 51.6 (spread 9%) | 149.1 (7%) | 205 |
| blocking | 64 B | 249.8 (spread 39%) | 184.2 (8%) | 431 |
| blocking | 128 B | 211.0 (spread 22%) | 188.8 (10%) | 610 |
128 B vs 64 B is within the run to run spread in every column. p99 is bimodal (about 200 ns or about 500 ns per round trip) from run to run, which is thread placement across the two performance clusters, not padding. Blocking beats spinning on stream throughput because a full or empty queue parks one side and the other runs a 1024 item burst without contention.
Validated under ThreadSanitizer and AddressSanitizer at both paddings: the test suite plus a reduced benchmark (2M ints, 50k round trips), with no reports.
include/lfds/net.hpp is a minimal blocking TCP layer over loopback: RAII Socket, listen_loopback(0) with bound_port, accept_one, connect_loopback, send_all (partial write loop), recv_some, and recv_frames, which reassembles fixed 16 byte frames (u64 seq, u64 payload) across partial reads. tests/test_spsc_tcp.cpp sends 200k frames in 1000 byte chunks so frames straddle recv boundaries, a receiver thread pushes them into a SpscRingBuffer<Frame>, and the consumer asserts the sequence is contiguous. It runs under the same TSan and ASan builds as the rest of the suite.
bench/spsc_tcp_bench.cpp times the same pipeline, sender, TCP loopback, receiver, ring, consumer, and is built as spsc_tcp_bench_64 and spsc_tcp_bench_128.
cmake --build build-bench
./build-bench/bench/spsc_tcp_bench_64 # stream and latency, 5 runs each
./build-bench/bench/spsc_tcp_bench_64 --mode latency --rounds 200000
stream: sender blasts 10M frames in 64 KiB writes, consumer pops and checksums. Reports M msgs/s end to end.
latency: one frame in flight at a time, stamped with mach_absolute_time on send and read on pop. Reports one way ns from send to pop, unloaded, so no socket buffer queueing is included.
Measured 2026-10-22 on an Apple M4 Max, GCC 16.2, 5 runs per mode, paddings run back to back rather than interleaved. Medians across runs, spread is (max - min) / median.
| padding | stream M msgs/s | one way p50 ns | one way p99 ns |
|---|---|---|---|
| 64 B | 82.6 (spread 62%) | 10792 (12%) | 25250 |
| 128 B | 118.2 (spread 21%) | 10209 (6%) | 19625 |
The stream numbers are bounded by the receiver thread doing recv plus push, not by the ring, and the 64 B run had one slow outlier, so the padding gap is within drift. One way latency is about 10 µs at p50 and is dominated by the kernel loopback wakeup, which is two orders of magnitude above the in-memory handoff (150 to 190 ns round trip).