https://github.com/alsch092/lockfree-circularbuffer
Lock-free circular buffer in C++ for fast & thread-safe game events
https://github.com/alsch092/lockfree-circularbuffer
circular-buffer lock-free-queue telemetry-collection
Last synced: 6 months ago
JSON representation
Lock-free circular buffer in C++ for fast & thread-safe game events
- Host: GitHub
- URL: https://github.com/alsch092/lockfree-circularbuffer
- Owner: AlSch092
- Created: 2025-09-23T20:56:19.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2025-09-23T21:40:58.000Z (6 months ago)
- Last Synced: 2025-09-23T23:27:20.040Z (6 months ago)
- Topics: circular-buffer, lock-free-queue, telemetry-collection
- Language: C++
- Homepage:
- Size: 5.86 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# LockFree-CircularBuffer
Lock-free circular buffer in C++ for fast & thread-safe game events
## What is this?
A circular buffer is a buffer with N-spaces, which can be thought of as being 'connected' as a circle, displaying similar properties to a double-ended queue. When items are pushed/written, the 'tail' moves one position forward, and when items are popped/read, the 'head' position moves forward once. When the buffer has all spots occupied, it can no longer be written to without being popped from. Because of using a write + read index, the occupied slots can be 'moved/slid around' the circle, not requiring the head element to be in the literal first index of the buffer. The head and tail can both be accessed as needed, making this type of structure beneficial for specific use cases.
This particular C++ implementation makes use of several beneficial aspects of the language, including atomics for the read/write indexes, alignment specifications (alignas, alignof + paddings for vectorization/CPU cache access), type templating, rvalue references and move semantics rather than value copying. This makes this type of implementation great for cross-thread/concurrent usage in games, where we need to push game events on a main thread and access these events quickly and safely from a 2nd consumer thread (to drain game events to a socket or file for telemetry).
A class `MPSC` is also included which uses the `CircularBuffer` class in a multi-threaded fashion, where each thread becomes a 'channel' to produce events on. A flush function combines events from all producer threads/channels and drains/writes each of them to a file (this can be changed to a socket or whatever else you like).
A brief example/test can be found in the `main()` function which shows usage of the `CircularBuffer` class on its own, along with the `MPSC` class using two threads, which drains their data to a file `vals.txt`.
Anyone is free to use the classes for whatever purpose, if there are any bugs I might be missing feel free to post an issue!