{"id":46290620,"url":"https://github.com/wizenink/bring","last_synced_at":"2026-03-04T08:08:39.301Z","repository":{"id":331435221,"uuid":"1126615486","full_name":"wizenink/bring","owner":"wizenink","description":"High-performance, lock-free SPSC ring buffer","archived":false,"fork":false,"pushed_at":"2026-01-05T10:18:01.000Z","size":36,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2026-01-08T08:26:02.474Z","etag":null,"topics":["concurrent","cpp","lock-free","ring-buffer","spsc"],"latest_commit_sha":null,"homepage":"","language":"CMake","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wizenink.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-01-02T09:06:44.000Z","updated_at":"2026-01-05T10:18:05.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/wizenink/bring","commit_stats":null,"previous_names":["wizenink/bring"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/wizenink/bring","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wizenink%2Fbring","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wizenink%2Fbring/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wizenink%2Fbring/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wizenink%2Fbring/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wizenink","download_url":"https://codeload.github.com/wizenink/bring/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wizenink%2Fbring/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30076027,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-04T08:01:56.766Z","status":"ssl_error","status_checked_at":"2026-03-04T08:00:42.919Z","response_time":59,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["concurrent","cpp","lock-free","ring-buffer","spsc"],"created_at":"2026-03-04T08:08:38.479Z","updated_at":"2026-03-04T08:08:39.269Z","avatar_url":"https://github.com/wizenink.png","language":"CMake","readme":"# Bring - Lock-Free SPSC Ring Buffer\n\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n[![C++23](https://img.shields.io/badge/C%2B%2B-23-blue.svg)](https://en.cppreference.com/w/cpp/23)\n\nA high-performance, lock-free **Single Producer Single Consumer (SPSC)** ring buffer implementation in modern C++23. Designed for maximum throughput and minimal latency in concurrent scenarios.\n\n## Features\n\n- **Lock-Free**: Uses atomics with acquire-release semantics for thread safety\n- **Cache-Optimized**: False sharing prevention with 64-byte alignment\n- **Zero-Copy**: Move semantics and in-place construction support\n- **Type-Safe**: C++23 concepts for compile-time validation\n- **Header-Only**: Easy integration, just include and use\n\n## Quick Start\n\n### Requirements\n\n- C++23 compatible compiler (GCC 12+, Clang 16+, MSVC 2022+)\n- CMake 3.20+\n\n### Installation\n\n#### Header-Only\n\nSimply copy `include/bring/ring_buffer.hpp` to your project:\n\n```cpp\n#include \u003cbring/ring_buffer.hpp\u003e\n\nbring::RingBuffer\u003cint, 1024\u003e buffer;\n```\n\n#### CMake Integration\n\n```cmake\ninclude(FetchContent)\nFetchContent_Declare(\n  bring\n  GIT_REPOSITORY https://github.com/wizenink/bring.git\n  GIT_TAG master\n)\nFetchContent_MakeAvailable(bring)\n\ntarget_link_libraries(your_target PRIVATE bring::bring)\n```\n\n### Basic Usage\n\n```cpp\n#include \u003cbring/ring_buffer.hpp\u003e\n#include \u003cthread\u003e\n\n// Create a ring buffer (capacity must be power of 2)\nbring::RingBuffer\u003cint, 64\u003e buffer;\n\n// Producer thread\nstd::thread producer([\u0026buffer]() {\n    for (int i = 0; i \u003c 1000; ++i) {\n        while (!buffer.try_push(i)) {\n            std::this_thread::yield(); // Buffer full, retry\n        }\n    }\n});\n\n// Consumer thread\nstd::thread consumer([\u0026buffer]() {\n    for (int i = 0; i \u003c 1000; ++i) {\n        auto value = buffer.try_pop();\n        while (!value.has_value()) {\n            std::this_thread::yield(); // Buffer empty, retry\n            value = buffer.try_pop();\n        }\n        // Process value.value()\n    }\n});\n\nproducer.join();\nconsumer.join();\n```\n\n## API Reference\n\n### Construction\n\n```cpp\nbring::RingBuffer\u003cT, Capacity\u003e buffer;\n```\n\n- `T`: Element type (must be move-constructible and destructible)\n- `Capacity`: Buffer size (must be power of 2, \u003e 1)\n\n### Core Operations\n\n#### `try_push(item) -\u003e bool`\n\nTry to push an item into the buffer. Returns `true` on success, `false` if buffer is full.\n\n```cpp\nif (buffer.try_push(42)) {\n    // Success\n}\n```\n\n#### `try_pop() -\u003e std::optional\u003cT\u003e`\n\nTry to pop an item from the buffer. Returns `std::optional` with value on success, `std::nullopt` if empty.\n\n```cpp\nauto result = buffer.try_pop();\nif (result.has_value()) {\n    int value = result.value();\n}\n```\n\n#### `try_pop_ip(T\u0026 out) -\u003e bool`\n\nIn-place pop for better performance (avoids `std::optional` overhead).\n\n```cpp\nint value;\nif (buffer.try_pop_ip(value)) {\n    // value contains popped element\n}\n```\n\n#### `try_consume(Func\u0026\u0026 processor) -\u003e bool`\n\nProcess element with callback without extracting it.\n\n```cpp\nbuffer.try_consume([](int\u0026\u0026 value) {\n    // Process value\n});\n```\n\n#### `emplace(Args\u0026\u0026... args) -\u003e bool`\n\nConstruct element in-place. Returns `true` on success.\n\n```cpp\nbuffer.emplace(arg1, arg2, arg3);\n```\n\n### Query Operations\n\n#### `is_empty() -\u003e bool`\n\nCheck if buffer is empty.\n\n#### `is_full() -\u003e bool`\n\nCheck if buffer is full.\n\n#### `get_state() -\u003e BufferState`\n\nAtomically check both empty and full state from a consistent snapshot.\n\n```cpp\nauto state = buffer.get_state();\nif (state.empty) {\n    // Buffer is empty\n}\nif (state.full) {\n    // Buffer is full\n}\n```\n\n## Performance\n\nBenchmarks show exceptional performance for SPSC scenarios:\n\n- **Single-threaded**: ~1-2 ns per push/pop operation\n- **Multi-threaded (SPSC)**: ~3-10 ns per operation depending on buffer size\n- **Cache performance**: \u003e99.999% L1 data cache hit rate (validated with cachegrind)\n- **Throughput**: Up to 300M+ operations/second on modern hardware\n\nRun `cmake --build build --target run_benchmarks` to see performance on your system.\n\n## Design Decisions\n\n### Why Power-of-2 Capacity?\n\nEnables fast modulo operations using bitwise AND: `(index + 1) \u0026 (Capacity - 1)`\n\n### Memory Ordering\n\n- **Acquire-Release**: Ensures visibility of data between producer/consumer\n- **Relaxed**: Used for same-thread operations where ordering not critical\n\n### Cache Line Alignment\n\nHead and tail pointers are aligned to 64-byte boundaries to prevent false sharing between producer and consumer threads.\n\n### One-Slot Reservation\n\nThe buffer reserves one slot to distinguish between full and empty states (when `head == tail`).\n\n## Building \u0026 Testing\n\n### Running Tests\n\n```bash\n# Configure with testing enabled\ncmake -B build -DBUILD_TESTING=ON\n\n# Build\ncmake --build build\n\n# Run all tests using CMake target\ncmake --build build --target run_tests\n\n# Or run tests individually\n./build/unit_tests        # Single-threaded tests\n./build/mt_tests          # Multi-threaded stress tests\n\n# Or use CTest\ncd build \u0026\u0026 ctest --output-on-failure\n```\n\n### Running Benchmarks\n\n```bash\n# Configure with benchmarks enabled\ncmake -B build -DBUILD_BENCHMARKS=ON -DCMAKE_BUILD_TYPE=Release\n\n# Build and run benchmarks using CMake target\ncmake --build build --target run_benchmarks\n\n# Or run directly\n./build/benchmarks\n\n# Run with specific parameters\n./build/benchmarks --benchmark_filter=BM_SPSC --benchmark_min_time=1.0s\n```\n\n### Cache Performance Analysis\n\n```bash\n# Build the cachegrind benchmark\ncmake -B build -DBUILD_TESTING=ON\n\n# Run with valgrind cachegrind\nvalgrind --tool=cachegrind --cachegrind-out-file=cachegrind.out ./build/cachegrind_bench\n\n# View results\ncg_annotate cachegrind.out | head -50\n```\n\n## Thread Safety\n\n**SPSC Only**: This ring buffer is designed for exactly one producer thread and one consumer thread. Using it with multiple producers or consumers will result in race conditions.\n\nFor MPSC/MPMC scenarios, use a different data structure or add external synchronization.\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwizenink%2Fbring","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwizenink%2Fbring","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwizenink%2Fbring/lists"}