{"id":51007307,"url":"https://github.com/tranglecong/trlc_threadsafe","last_synced_at":"2026-06-20T21:32:03.580Z","repository":{"id":258765741,"uuid":"875498106","full_name":"tranglecong/trlc_threadsafe","owner":"tranglecong","description":"A C++ library that provides utilities for managing thread-safe operations. This library is designed to simplify concurrent programming by offering easy-to-use and robust thread-safe abstractions.","archived":false,"fork":false,"pushed_at":"2024-10-30T17:29:41.000Z","size":636,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-10-30T18:18:45.768Z","etag":null,"topics":["cpp","guard","mutilthread","notify","queue","wait"],"latest_commit_sha":null,"homepage":"https://tranglecong.github.io/trlc_threadsafe","language":"C++","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/tranglecong.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}},"created_at":"2024-10-20T06:18:01.000Z","updated_at":"2024-10-30T17:27:13.000Z","dependencies_parsed_at":"2024-10-23T13:32:39.455Z","dependency_job_id":null,"html_url":"https://github.com/tranglecong/trlc_threadsafe","commit_stats":null,"previous_names":["tranglecong/thread_safe","tranglecong/trlc_threadsafe"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/tranglecong/trlc_threadsafe","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tranglecong%2Ftrlc_threadsafe","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tranglecong%2Ftrlc_threadsafe/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tranglecong%2Ftrlc_threadsafe/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tranglecong%2Ftrlc_threadsafe/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tranglecong","download_url":"https://codeload.github.com/tranglecong/trlc_threadsafe/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tranglecong%2Ftrlc_threadsafe/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34586666,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-20T02:00:06.407Z","response_time":98,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["cpp","guard","mutilthread","notify","queue","wait"],"created_at":"2026-06-20T21:32:02.918Z","updated_at":"2026-06-20T21:32:03.572Z","avatar_url":"https://github.com/tranglecong.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TRLC-ThreadSafe\n\n[![CI](https://github.com/tranglc/trlc-threadsafe/actions/workflows/ci.yml/badge.svg)](https://github.com/tranglc/trlc-threadsafe/actions/workflows/ci.yml)\n\nModern C++ concurrency library using **policy-based design** for composable, high-performance multithreading primitives.\n\n## Features\n\n- **Header-Only**: Zero build dependencies, just include and use\n- **Policy-Based Design**: Compose components with compile-time policies\n- **Type-Safe**: Leverage C++17 templates for compile-time safety\n- **High Performance**: Lock-free algorithms, cache-aligned data structures\n- **Cross-Platform**: Linux, macOS, Windows support\n\n## Library Components\n\n### Containers\n- `BoundedQueue\u003cT, N\u003e` - Fixed-size MPMC queue with backpressure\n- `UnboundedQueue\u003cT\u003e` - Dynamic MPMC queue\n- `Channel\u003cT\u003e` - Go-style message passing (buffered/unbuffered)\n- `ConcurrentMap\u003cK, V\u003e` - Sharded hash map for concurrent access\n\n### Synchronization\n- `Guarded\u003cT\u003e` - Mutex-protected data with scoped locking\n- `RwGuarded\u003cT\u003e` - Reader-writer lock wrapper\n- `Event` - Manual/auto-reset signaling primitive\n- `Notification` - One-shot signaling\n- `WaitGroup` - Go-style synchronization\n\n### Lock-Free\n- `SPSCQueue\u003cT, N\u003e` - Wait-free single-producer, single-consumer queue\n- `MPSCQueue\u003cT\u003e` - Lock-free multi-producer, single-consumer queue\n- `WorkStealingDeque\u003cT\u003e` - Chase-Lev work-stealing deque\n\n### Execution\n- `ThreadPool` - Work-stealing thread pool with task submission\n- `Pipeline\u003cIn, Out\u003e` - Staged data processing with backpressure\n- `EventBus\u003cEvents...\u003e` - Type-safe publish-subscribe with policies\n- `ActorSystem` - Message-based concurrency\n\n### Foundation\n- `Result\u003cT, E\u003e` - Rust-style error handling with monadic operations\n- `Option\u003cT\u003e` - Maybe/Optional with monadic operations\n- `Status` - gRPC-style status codes\n\n## Examples\n\n\u003e **💡 Complete working examples in `examples/showcase/`:**\n\u003e - `result_railway.cpp` - Railway-oriented programming with monadic operations\n\u003e - `eventbus_policies.cpp` - Type-erased execution policies (inline/threadpool)\n\u003e - `channel_select.cpp` - Go-style CSP with select multiplexing\n\u003e - `queue_overflow.cpp` - 5 overflow policies demonstration\n\u003e - `pipeline_backpressure.cpp` - Multi-stage processing with backpressure\n\u003e - `threadpool_work_stealing.cpp` - Work-stealing load balancing\n\u003e\n\u003e See `examples/` directory for 40+ complete examples covering all components.\n\n### 1. Result - Railway-Oriented Programming\n\n```cpp\n#include \u003ctrlc/threadsafe/core/error/result.hpp\u003e\n\nenum class Error { ParseError, ValidationError, NetworkError };\n\n// Chain operations - errors automatically short-circuit\nauto processRequest(const std::string\u0026 json) {\n    return parseJSON(json)                    // Result\u003cData, Error\u003e\n        .andThen(validateSchema)              // Only runs if parse succeeded\n        .andThen(enrichWithUserData)          // Only runs if validation passed\n        .map([](Data d) { return d.value * 2; })  // Transform success\n        .orElse([](Error e) {                 // Fallback on any error\n            log(\"Failed: {}\", e);\n            return loadCachedData();\n        });\n}\n\n// Pattern matching for exhaustive handling\nauto result = processRequest(request_body);\nresult.match(\n    [](int value) { sendResponse(200, value); },\n    [](Error e) { sendResponse(500, errorMsg(e)); }\n);\n\n// Monadic operations: map, andThen, orElse, filter\nauto age = getUser(id)\n    .map([](User u) { return u.age; })\n    .filter([](int age) { return age \u003e= 18; })\n    .valueOr(0);\n```\n\n---\n\n### 2. EventBus - Pub-Sub with Execution Policies\n\n```cpp\n#include \u003ctrlc/threadsafe/patterns/event_bus.hpp\u003e\n#include \u003ctrlc/threadsafe/exec/thread_pool.hpp\u003e\n\nstruct SensorData { int id; double temp; };\nstruct Alert { std::string msg; };\n\n// Inline: \u003c1μs, runs on publisher thread\nauto ui_bus = makeInlineEventBus\u003cSensorData, Alert\u003e();\n\n// ThreadPool: ~100μs, async execution\nauto pool = std::make_shared\u003cexec::ThreadPool\u003e(4);\nauto io_bus = makeThreadPoolEventBus\u003cSensorData, Alert\u003e(pool);\n\n// Subscribe - multiple handlers, decoupled\nauto h1 = ui_bus-\u003esubscribe\u003cSensorData\u003e([](const SensorData\u0026 d) {\n    dashboard.update(d);  // Fast UI update\n});\n\nauto h2 = io_bus-\u003esubscribe\u003cSensorData\u003e([](const SensorData\u0026 d) {\n    database.save(d);     // Heavy I/O - don't block!\n    if (d.temp \u003e 80) {\n        sendAlert({\"Overheat: \" + std::to_string(d.temp)});\n    }\n});\n\nauto h3 = io_bus-\u003esubscribe\u003cAlert\u003e([](const Alert\u0026 a) {\n    smtp.send(a.msg);     // Network call - async\n});\n\n// Publish - fire and forget\nui_bus-\u003epublish(SensorData{1, 75.5});   // Runs immediately\nio_bus-\u003epublish(SensorData{2, 85.2});   // Queued, returns instantly\n\n// RAII: auto-unsubscribe when handles destroyed\npool-\u003eshutdown();\n```\n\n---\n\n### 3. Channel - Go-Style CSP\n\n```cpp\n#include \u003ctrlc/threadsafe/containers/channel.hpp\u003e\n\n// Unbuffered: Rendezvous (both sides must be ready)\nChannel\u003cRequest, Unbuffered\u003e rpc;\nstd::thread server([\u0026] {\n    auto req = rpc.recv();        // Blocks until client sends\n    auto res = process(req.value());\n    response_ch.send(res);        // Client is waiting\n});\nrpc.send(Request{\"data\"});        // Synchronization point\n\n// Buffered: Producer-consumer with automatic backpressure\nChannel\u003cImage, Bounded\u003c8\u003e\u003e images;\nstd::thread producer([\u0026] {\n    for (auto\u0026 img : camera.stream()) {\n        images.send(img);         // Blocks if buffer full\n    }\n    images.close();               // Signal completion\n});\n\nstd::thread consumer([\u0026] {\n    while (auto img = images.recv(); img.isOk()) {\n        processImage(img.value());\n    }\n});\n\n// Select pattern: Multiplexing multiple channels\nChannel\u003cint, Bounded\u003c4\u003e\u003e ch1, ch2, ch3;\nwhile (running) {\n    auto r1 = ch1.tryRecv();\n    auto r2 = ch2.tryRecv();\n    auto r3 = ch3.tryRecv();\n    \n    if (r1.isOk()) handleSource1(r1.value());\n    if (r2.isOk()) handleSource2(r2.value());\n    if (r3.isOk()) handleSource3(r3.value());\n}\n\n// Timeout operations\nauto result = ch1.recvFor(100ms);\nif (result.isErr() \u0026\u0026 result.error() == ChannelStatus::timeout) {\n    handleTimeout();\n}\n```\n\n---\n\n### 4. BoundedQueue - 5 Overflow Policies\n\n```cpp\n#include \u003ctrlc/threadsafe/containers/bounded_queue.hpp\u003e\n\n// Block: Waits when full (critical data - orders, payments)\nBoundedQueue\u003cOrder, 128\u003e orders;\norders.push(order);  // Blocks if full\n\n// Fail: Returns error (need to know capacity reached)\nBoundedQueue\u003cRequest, 128, OverflowPolicy::Fail\u003e requests;\nif (auto res = requests.tryPush(req); res.isErr()) {\n    metrics.queueFull++;\n    return Error::ServiceOverloaded;\n}\n\n// DropOldest: Keep latest (sensor data, stock prices)\nBoundedQueue\u003cTemperature, 64, OverflowPolicy::DropOldest\u003e sensors;\nsensors.tryPush(temp);  // Always succeeds, drops old data\n\n// DropNewest: Keep history (audit logs, telemetry)\nBoundedQueue\u003cAuditLog, 256, OverflowPolicy::DropNewest\u003e logs;\nlogs.tryPush(log);  // Keeps old logs if full\n\n// Overwrite: Ring buffer (video frames, audio samples)\nBoundedQueue\u003cFrame, 32, OverflowPolicy::Overwrite\u003e frames;\nframes.tryPush(frame);  // Overwrites oldest position\n\n// Consumer (same for all policies)\nwhile (auto item = orders.tryPop(); item.isOk()) {\n    process(item.value());\n}\n\n// MPMC safe: Multiple producers and consumers\nstd::thread t1([\u0026] { orders.push(Order{1}); });\nstd::thread t2([\u0026] { orders.push(Order{2}); });\nstd::thread c1([\u0026] { orders.tryPop(); });\nstd::thread c2([\u0026] { orders.tryPop(); });\n```\n\n---\n\n### 5. Pipeline - Multi-Stage Processing\n\n```cpp\n#include \u003ctrlc/threadsafe/patterns/pipeline.hpp\u003e\n\n// Image processing: Decode → Filter → Encode\nPipeline\u003cImageData, CompressedImage\u003e pipeline;\n\n// Stage 1: Decode (2 workers)\nPipeline\u003cImageData, CompressedImage\u003e::StageConfig cfg1;\ncfg1.workers = 2;\ncfg1.buffer_size = 16;\npipeline.addStage\u003cImageData, RawImage\u003e(\n    [](const ImageData\u0026 img) { return decode(img); },\n    cfg1\n);\n\n// Stage 2: Apply filters (4 workers - GPU bound)\nPipeline\u003cImageData, CompressedImage\u003e::StageConfig cfg2;\ncfg2.workers = 4;\ncfg2.buffer_size = 32;\npipeline.addStage\u003cRawImage, FilteredImage\u003e(\n    [](const RawImage\u0026 raw) { return applyFilters(raw); },\n    cfg2\n);\n\n// Stage 3: Compress (2 workers)\nPipeline\u003cImageData, CompressedImage\u003e::StageConfig cfg3;\ncfg3.workers = 2;\ncfg3.buffer_size = 16;\npipeline.addStage\u003cFilteredImage, CompressedImage\u003e(\n    [](const FilteredImage\u0026 f) { return compress(f); },\n    cfg3\n);\n\npipeline.start();\n\n// Producer: Feed data (automatic backpressure)\nfor (auto\u0026 img : camera.capture()) {\n    pipeline.submit(img);  // Blocks if stage 1 buffer full\n}\n\n// Consumer: Get results\nwhile (auto result = pipeline.receive(); result.isOk()) {\n    network.send(result.value());\n}\n\npipeline.stop();\n```\n\n---\n\n### 6. ThreadPool - Work Stealing\n\n```cpp\n#include \u003ctrlc/threadsafe/exec/thread_pool.hpp\u003e\n\nThreadPool pool(std::thread::hardware_concurrency());\n\n// Submit 10,000 tasks\nstd::atomic\u003cint\u003e completed{0};\nstd::vector\u003cstd::future\u003cResult\u003e\u003e futures;\n\nfor (int i = 0; i \u003c 10000; ++i) {\n    futures.push_back(pool.submit([\u0026completed, i]() {\n        auto result = processFile(files[i]);  // Varying duration\n        completed.fetch_add(1);\n        return result;\n    }));\n}\n\n// Work stealing: Fast threads steal from busy threads\n// No manual load balancing needed!\n\npool.waitIdle();\nstd::cout \u003c\u003c \"Completed: \" \u003c\u003c completed.load() \u003c\u003c std::endl;\n\n// Check results\nfor (auto\u0026 f : futures) {\n    if (auto res = f.get(); res.isErr()) {\n        handleError(res.error());\n    }\n}\n\npool.shutdown();\n\n// Fire-and-forget style\nfor (int i = 0; i \u003c 1000; ++i) {\n    pool.submit([i]() {\n        processTask(i);  // \u003c100ns submission latency\n    });\n}\n```\n\n---\n\n### 7. Option \u0026 Result - Monadic Operations\n\n```cpp\n#include \u003ctrlc/threadsafe/core/error/option.hpp\u003e\n#include \u003ctrlc/threadsafe/core/error/result.hpp\u003e\n\n// Option: Nullable values with monadic operations\nOption\u003cUser\u003e findUser(int id);\n\nauto name = findUser(123)\n    .map([](const User\u0026 u) { return u.name; })\n    .filter([](auto\u0026 n) { return !n.empty(); })\n    .valueOr(\"Guest\");\n\nif (auto user = findUser(123); user.isSome()) {\n    process(user.value());\n}\n\n// Result: Error handling with context\nenum class Error { NotFound, ParseError, ValidationError };\n\nResult\u003cConfig, Error\u003e loadConfig() {\n    return readFile(\"config.json\")\n        .andThen(parseJSON)\n        .andThen(validateConfig)\n        .orElse([](Error e) {\n            log(\"Config error: {}\", e);\n            return loadDefaults();\n        });\n}\n\nloadConfig().match(\n    [](const Config\u0026 c) { app.init(c); },\n    [](Error e) { app.initMinimal(); }\n);\n\n// Compose multiple operations\nauto result = getUser(id)\n    .andThen(checkPermissions)\n    .andThen(loadProfile)\n    .map([](Profile p) { return p.settings; })\n    .orElse([](Error e) { return defaultSettings(); });\n```\n\n---\n\n### 8. Guarded - RAII Mutex\n\n```cpp\n#include \u003ctrlc/threadsafe/sync/guarded.hpp\u003e\n\nstruct Account { double balance; };\nGuarded\u003cAccount\u003e account{Account{1000.0}};\n\n// Scoped lock with lambda\naccount.with([](Account\u0026 acc) {\n    acc.balance -= 100.0;\n});  // Auto-unlock\n\n// Manual lock guard\n{\n    auto guard = account.lock();\n    guard-\u003ebalance += 50.0;\n}  // Auto-unlock\n\n// Try-lock (non-blocking) - returns Option\u003cGuard\u003e\nauto maybe_guard = account.tryLock();\nif (maybe_guard.isSome()) {\n    maybe_guard.value()-\u003ebalance += 10.0;\n} else {\n    metrics.lockContentions++;\n}\n\n// Timeout lock\nauto guard = account.tryLockFor(100ms);\nguard.map([](auto\u0026 g) {\n    g-\u003ebalance -= 25.0;\n    return true;\n}).valueOr([]() {\n    log(\"Lock timeout\");\n    return false;\n});\n\n// Multi-threaded access\nstd::thread t1([\u0026] { account.with([](auto\u0026 a) { a.balance += 100; }); });\nstd::thread t2([\u0026] { account.with([](auto\u0026 a) { a.balance -= 50; }); });\n```\n\n---\n\n### 9. ConcurrentMap - Sharded HashMap\n\n```cpp\n#include \u003ctrlc/threadsafe/containers/concurrent_map.hpp\u003e\n\nConcurrentMap\u003cSessionId, UserSession\u003e sessions;  // 16 shards\n\n// Insert (returns Option\u003cV\u003e - old value if key existed)\nauto old = sessions.insert(\"sess_123\", UserSession{...});\nif (old.isSome()) {\n    log(\"Replaced session: {}\", old.value().user_id);\n}\n\n// Get (returns Option\u003cV\u003e)\nauto session = sessions.get(\"sess_123\");\nif (session.isSome()) {\n    process(session.value());\n} else {\n    createNewSession();\n}\n\n// Update with lambda (atomic)\nsessions.update(\"sess_123\", [](UserSession\u0026 s) {\n    s.last_activity = now();\n    s.request_count++;\n});  // Only this shard locked\n\n// Remove (returns Option\u003cV\u003e)\nauto removed = sessions.remove(\"sess_123\");\nremoved.map([](const UserSession\u0026 s) {\n    cleanup(s);\n});\n\n// Lock-free size\nstd::cout \u003c\u003c \"Active: \" \u003c\u003c sessions.size() \u003c\u003c std::endl;\n\n// Concurrent access from multiple threads\nstd::thread t1([\u0026] { sessions.insert(\"s1\", UserSession{}); });\nstd::thread t2([\u0026] { sessions.insert(\"s2\", UserSession{}); });\nstd::thread t3([\u0026] { sessions.get(\"s1\"); });\nstd::thread t4([\u0026] { sessions.update(\"s2\", [](auto\u0026 s) { s.hits++; }); });\n```\n---\n\n## Quick Start\n\n### Prerequisites\n\n- C++17 compatible compiler (GCC 9+, Clang 11+, MSVC 2019+)\n- CMake 3.15+\n- GoogleTest (automatically downloaded)\n\n### Building from Source\n\n**Linux/macOS:**\n```bash\ncmake -B build -S . -DCMAKE_BUILD_TYPE=Release\ncmake --build build --parallel\ncd build \u0026\u0026 ctest --output-on-failure\n```\n\n**Windows:**\n```bash\ncmake -B build -S .\ncmake --build build --config Release --parallel\ncd build\nctest -C Release --output-on-failure\n```\n\n### Using in Your Project\n\n**Header-only (recommended):**\n```cmake\n# In your CMakeLists.txt\nadd_subdirectory(external/trlc-threadsafe)\ntarget_link_libraries(your_target PRIVATE trlc::threadsafe)\n```\n\n**Include in code:**\n```cpp\n#include \u003ctrlc/threadsafe/containers/bounded_queue.hpp\u003e\n#include \u003ctrlc/threadsafe/patterns/event_bus.hpp\u003e\n#include \u003ctrlc/threadsafe/exec/thread_pool.hpp\u003e\n\nusing namespace trlc::threadsafe;\n```\n\n\n## Contributing\n\nContributions are welcome! Please follow these guidelines:\n\n1. Fork the repository\n2. Create a feature branch (`git checkout -b feature/amazing-feature`)\n3. Make your changes with tests\n4. Ensure all tests pass locally (`./scripts/test-all-compilers.sh`)\n5. Commit with descriptive messages (`git commit -m \"Add amazing feature\"`)\n6. Push to your branch (`git push origin feature/amazing-feature`)\n7. Open a Pull Request\n\n## License\n\nMIT License - see [LICENSE](LICENSE) file for details.\n\n## Contact\n\n- **Author**: tranglc\n- **Repository**: [https://github.com/tranglc/trlc-threadsafe](https://github.com/tranglc/trlc-threadsafe)\n- **Issues**: [GitHub Issues](https://github.com/tranglc/trlc-threadsafe/issues)\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftranglecong%2Ftrlc_threadsafe","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftranglecong%2Ftrlc_threadsafe","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftranglecong%2Ftrlc_threadsafe/lists"}