A lock-free, multi-source capture buffer in C++20. Each source (a camera, a sensor, anything that produces timestamped events) writes into its own fixed-size ring; one merger thread orders every source's events onto a single timeline. On a pause the rings freeze, and a capture window gives a zero-copy, read-only view of any time range across every source.
MIT licensed. No dependencies beyond the C++ standard library and the platform's threads.
EventBuffer: register up to 16 sources, write from each source's own thread (acquireWriteSlot→ fill →publish), pause, resume, stop. Producers never block and never allocate.- Seqlock rings (
SourceRing): a reader can always detect a slot that was overwritten mid-read. - A merged timeline (
TimelineIndex), with a reorder window so sources whose events arrive slightly late still interleave in timestamp order, andSubscriptions to follow it live. CaptureWindow: a frozen time range.entriesFor(id),payloadOf(entry), andbracket(id, t), the entries either side of an instant. A window built from the buffer pins every ring registered at the pause, so a source may be deregistered while the window is still read; the buffer refuses to resume until the window goes.PayloadSource: the window's backing is pluggable, so a host can build the same window over data it holds itself (in RAM, or streamed from a file).- A stall watchdog, per-source statistics and a diagnostics snapshot.
DeviceClockMapper: maps a device's own frame clock onto the host clock (a lower-envelope fit with a rolling window), for devices that report their own capture instant.
The buffer stores no format metadata. A host that needs it (pixel formats, sample rates) keeps a table keyed by
SourceId.
add_subdirectory(libbuffer) # or FetchContent
target_link_libraries(app PRIVATE libbuffer::libbuffer)#include <libbuffer/event_buffer.h>
libbuffer::EventBuffer buf;
libbuffer::SourceConfig cfg;
cfg.name = "sensor"; cfg.slot_count = 1000; cfg.slot_bytes = 32;
const auto id = buf.registerSource(cfg);
buf.start();
// on the sensor's thread
auto slot = buf.acquireWriteSlot(id);
if (slot.valid) {
std::memcpy(slot.data, sample, 32);
*slot.bytes_written = 32;
*slot.timestamp_us = libbuffer::EventBuffer::nowMicros();
buf.publish(id, slot.sequence);
}
// later, on any thread
buf.pause();
{
auto window = buf.captureWindow(std::chrono::milliseconds(2000));
for (const auto& e : window.entriesFor(id)) {
auto h = window.payloadOf(e);
// ... read h.data, h.bytes ...
}
}
buf.resume();Embedded, the library builds itself and nothing else: no tests, no -Werror, no install rules.
cmake --preset dev && cmake --build build/dev && ctest --preset dev
cmake --preset san && cmake --build build/san && ctest --preset san # ASan + UBSan
cmake --preset tsan && cmake --build build/tsan && ctest --preset tsan # TSan
cmake --preset release && cmake --build build/release && build/release/tests/latency_benchmarkThe sanitizers are LIBBUFFER_ENABLE_ASAN, LIBBUFFER_ENABLE_UBSAN and LIBBUFFER_ENABLE_TSAN (TSan on its own), and
LIBBUFFER_ENABLE_COVERAGE builds with gcov (the cov preset). The seqlock's deliberate, validated races are
suppressed by name in tests/tsan_suppressions.txt; the test targets set TSAN_OPTIONS for you.
no_host_names (a ctest, and a CI step) keeps the library general: it fails on the names of the application it was
taken from anywhere in include/ and src/.
docs/design.md explains the rings, the merger, the window and the platform-specific waits.