{"id":49353756,"url":"https://github.com/leventkaragol/libcpp-channel","last_synced_at":"2026-04-27T11:32:12.689Z","repository":{"id":240917951,"uuid":"803602088","full_name":"leventkaragol/libcpp-channel","owner":"leventkaragol","description":"Thread-safe generic message channel library for C++ (17+)","archived":false,"fork":false,"pushed_at":"2024-10-20T09:51:53.000Z","size":23,"stargazers_count":1,"open_issues_count":5,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-17T00:37:34.995Z","etag":null,"topics":["channels","concurrency","data-exchange","data-pipeline","decoupling","fifo","header-only","inter-thread","inter-thread-communication","message-channel","message-queue","producer-consumer","queues","task-queue","thread-safe"],"latest_commit_sha":null,"homepage":"","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/leventkaragol.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-05-21T03:28:27.000Z","updated_at":"2024-10-20T09:51:56.000Z","dependencies_parsed_at":"2024-05-21T13:01:14.498Z","dependency_job_id":"55f62bb9-4f83-4217-af4e-823e8cc5ad29","html_url":"https://github.com/leventkaragol/libcpp-channel","commit_stats":null,"previous_names":["leventkaragol/libcpp-channel"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/leventkaragol/libcpp-channel","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leventkaragol%2Flibcpp-channel","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leventkaragol%2Flibcpp-channel/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leventkaragol%2Flibcpp-channel/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leventkaragol%2Flibcpp-channel/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/leventkaragol","download_url":"https://codeload.github.com/leventkaragol/libcpp-channel/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leventkaragol%2Flibcpp-channel/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32335296,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-26T23:26:28.701Z","status":"online","status_checked_at":"2026-04-27T02:00:06.769Z","response_time":128,"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":["channels","concurrency","data-exchange","data-pipeline","decoupling","fifo","header-only","inter-thread","inter-thread-communication","message-channel","message-queue","producer-consumer","queues","task-queue","thread-safe"],"created_at":"2026-04-27T11:32:11.651Z","updated_at":"2026-04-27T11:32:12.683Z","avatar_url":"https://github.com/leventkaragol.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# libcpp-channel\n\nThread-safe generic message channel library for C++ (17+)\n\n[![linux](https://github.com/leventkaragol/libcpp-channel/actions/workflows/linux.yml/badge.svg)](https://github.com/leventkaragol/libcpp-channel/actions/workflows/linux.yml)\n[![windows](https://github.com/leventkaragol/libcpp-channel/actions/workflows/windows.yml/badge.svg)](https://github.com/leventkaragol/libcpp-channel/actions/workflows/windows.yml)\n\n\u003e [!TIP]\n\u003e Please read this document before using the library. I know, you don't have time but reading\n\u003e this document will save you time. I mean just this file, it's not long at all. Trial and error\n\u003e will cost you more time.\n\n# Table of Contents\n\n* [What is the channel used for and why do I need it?](#what-is-the-channel-used-for-and-why-do-i-need-it)\n* [How to add it to my project](#how-to-add-it-to-my-project)\n* [How to use? (Single Producer \u0026 Single Consumer)](#how-to-use-single-producer--single-consumer)\n* [How to use? (Single Producer \u0026 Multiple Consumers)](#how-to-use-single-producer--multiple-consumers)\n* [How to use? (Multiple Producers \u0026 Single Consumer)](#how-to-use-multiple-producers--single-consumer)\n* [How to use? (Multiple Producers \u0026 Multiple Consumers)](#how-to-use-multiple-producers--multiple-consumers)\n* [Semantic Versioning](#semantic-versioning)\n* [License](#license)\n* [Contact](#contact)\n\n## What is the channel used for and why do I need it?\n\nIn developed applications, there is often a need to exchange messages between different parts of the code. When these \ncode blocks are not directly connected and run on separate threads, managing communication between them becomes \nmore challenging, especially if there are multiple producers and consumers of messages. In such cases, channels are \nstructures used to facilitate communication between independent blocks of code in a thread-safe manner. While they may \nresemble events in appearance, their operating logic is more akin to message queues.\n\nHere are a few situations where you might need channels:\n\n* **Thread-Safe Data Exchange is Required**: When you need to ensure thread-safe communication between multiple threads\n* **Decoupling Producers and Consumers**: Decouple the producers and consumers of data, allowing them to operate independently\n* **Managing Multiple Producers and Consumers**: When you have multiple threads producing and consuming data, and you need a mechanism to manage their interactions\n* **Implementing FIFO Message Queues**: Ensure that messages are processed in the order they were received\n* **Simplifying Synchronization**: Simplify the synchronization logic in your application, avoiding complex locking mechanisms\n* **Improving Parallelism**: Distribute tasks across multiple threads, enhancing parallelism and performance\n* **Handling Real-Time Data**: Manage the quick and secure transmission of real-time data between threads\n* **Designing Scalable Systems**: Build scalable systems where components can be added or removed without affecting other parts of the system\n\n## How to add it to my project?\n\nThis is a header only library with no external dependency. So actually, all you need is to add the libcpp-channel.hpp file\nin src folder to your project and start using it with #include.\n\nYou can find usage examples in the examples folder, also find a sample CMakeLists.txt file content below.\n\n```cmake\ncmake_minimum_required(VERSION 3.14)\n\nproject(myProject)\n\nadd_executable(myProject main.cpp libcpp-channel.hpp)\n\n```\n\n## How to use? (Single Producer \u0026 Single Consumer)\n\nAs an easiest usage example, sending a message from a single producer and receiving the message \nby a single consumer can be done as follows.\n\n```cpp\n#include \"libcpp-channel.hpp\"\n#include \u003ciostream\u003e\n#include \u003cthread\u003e\n\nusing namespace lklibs;\n\nvoid produce(Channel\u003cstd::string\u003e::Producer producer)\n{\n    auto i = 0;\n\n    while (true)\n    {\n        i++;\n\n        // Sending string message to the consumer\n        producer.send(\"Message \" + std::to_string(i));\n\n        std::this_thread::sleep_for(std::chrono::milliseconds(1000));\n    }\n}\n\nvoid consume(Channel\u003cstd::string\u003e::Consumer consumer)\n{\n    while (true)\n    {\n        // Receiving message from the producer\n        auto message = consumer.receive();\n\n        if (message.has_value())\n        {\n            std::cout \u003c\u003c \"Consumer Received: \" \u003c\u003c message.value() \u003c\u003c std::endl;\n        }\n    }\n}\n\nint main()\n{\n    // Creating a string channel\n    Channel\u003cstd::string\u003e channel;\n\n    // Getting producer and consumer objects\n    auto producer = channel.getProducer();\n    auto consumer = channel.getConsumer();\n\n    // Passing producer object to the first thread\n    std::thread produce_thread(::produce, std::move(producer));\n\n    // Passing consumer object to the second thread\n    std::thread consume_thread(::consume, std::move(consumer));\n\n    produce_thread.join();\n    consume_thread.join();\n\n    return 0;\n}\n```\n\n## How to use? (Single Producer \u0026 Multiple Consumers)\n\nIn the example below, the message sent from a single producer is received concurrently by two \ndifferent consumers.\n\n```cpp\n#include \"libcpp-channel.hpp\"\n#include \u003ciostream\u003e\n#include \u003cthread\u003e\n\nusing namespace lklibs;\n\nvoid produce(Channel\u003cstd::string\u003e::Producer producer)\n{\n    auto i = 0;\n\n    while (true)\n    {\n        i++;\n\n        // Sending string message to all consumers\n        producer.send(\"Message \" + std::to_string(i));\n\n        std::this_thread::sleep_for(std::chrono::milliseconds(1000));\n    }\n}\n\nvoid consume(Channel\u003cstd::string\u003e::Consumer consumer, const std::string\u0026 name)\n{\n    while (true)\n    {\n        // Receiving message from the producer\n        auto message = consumer.receive();\n\n        if (message.has_value())\n        {\n            std::cout \u003c\u003c name \u003c\u003c \" Received: \" \u003c\u003c message.value() \u003c\u003c std::endl;\n        }\n    }\n}\n\nint main()\n{\n    // Creating a string channel\n    Channel\u003cstd::string\u003e channel;\n\n    // Getting producer and consumer objects\n    auto producer = channel.getProducer();\n    auto consumer1 = channel.getConsumer();\n    auto consumer2 = channel.getConsumer();\n\n    // Passing producer object to the first thread\n    std::thread produce_thread(::produce, std::move(producer));\n\n    // Passing consumer objects to other threads\n    std::thread consume1_thread(::consume, std::move(consumer1), \"Consumer1\");\n    std::thread consume2_thread(::consume, std::move(consumer2), \"Consumer2\");\n\n    produce_thread.join();\n    consume1_thread.join();\n    consume2_thread.join();\n\n    return 0;\n}\n```\n\n## How to use? (Multiple Producers \u0026 Single Consumer)\n\nAs a different use case, here is an example of two different producers sending messages to the \nsame channel and a single consumer receiving those messages.\n\n```cpp\n#include \"libcpp-channel.hpp\"\n#include \u003ciostream\u003e\n#include \u003cthread\u003e\n\nusing namespace lklibs;\n\nvoid produce(Channel\u003cstd::string\u003e::Producer producer, const std::string\u0026 name)\n{\n    auto i = 0;\n\n    while (true)\n    {\n        i++;\n\n        // Sending string message with producer name to the consumer\n        producer.send(name + \" Message \" + std::to_string(i));\n\n        std::this_thread::sleep_for(std::chrono::milliseconds(1000));\n    }\n}\n\nvoid consume(Channel\u003cstd::string\u003e::Consumer consumer)\n{\n    while (true)\n    {\n        // Receiving message from producers\n        auto message = consumer.receive();\n\n        if (message.has_value())\n        {\n            std::cout \u003c\u003c \"Consumer Received: \" \u003c\u003c message.value() \u003c\u003c std::endl;\n        }\n    }\n}\n\nint main()\n{\n    // Creating a string channel\n    Channel\u003cstd::string\u003e channel;\n\n    // Getting producer and consumer objects\n    auto producer1 = channel.getProducer();\n    auto producer2 = channel.getProducer();\n    auto consumer = channel.getConsumer();\n\n    // Passing producer objects to produce threads\n    std::thread produce1_thread(::produce, std::move(producer1), \"Producer1\");\n    std::thread produce2_thread(::produce, std::move(producer2), \"Producer2\");\n\n    // Passing consumer object to the consume thread\n    std::thread consume_thread(::consume, std::move(consumer));\n\n    produce1_thread.join();\n    produce2_thread.join();\n    consume_thread.join();\n\n    return 0;\n}\n```\n\n## How to use? (Multiple Producers \u0026 Multiple Consumers)\n\nIn the example below, two different producers send messages to the same channel independently of \neach other, while two different consumers receive these messages independently and concurrently.\n\n```cpp\n#include \"libcpp-channel.hpp\"\n#include \u003ciostream\u003e\n#include \u003cthread\u003e\n\nusing namespace lklibs;\n\nvoid produce(Channel\u003cstd::string\u003e::Producer producer, const std::string\u0026 name)\n{\n    auto i = 0;\n\n    while (true)\n    {\n        i++;\n\n        // Sending string message with producer name to all consumers\n        producer.send(name + \" Message \" + std::to_string(i));\n\n        std::this_thread::sleep_for(std::chrono::milliseconds(1000));\n    }\n}\n\nvoid consume(Channel\u003cstd::string\u003e::Consumer consumer, const std::string\u0026 name)\n{\n    while (true)\n    {\n        // Receiving message from producers\n        auto message = consumer.receive();\n\n        if (message.has_value())\n        {\n            std::cout \u003c\u003c name \u003c\u003c \" Received: \" \u003c\u003c message.value() \u003c\u003c std::endl;\n        }\n    }\n}\n\nint main()\n{\n    // Creating a string channel\n    Channel\u003cstd::string\u003e channel;\n\n    // Getting producer and consumer objects\n    auto producer1 = channel.getProducer();\n    auto producer2 = channel.getProducer();\n    auto consumer1 = channel.getConsumer();\n    auto consumer2 = channel.getConsumer();\n\n    // Passing producer objects to produce threads\n    std::thread produce1_thread(::produce, std::move(producer1), \"Producer1\");\n    std::thread produce2_thread(::produce, std::move(producer2), \"Producer2\");\n\n    // Passing consumer object to the consume threads\n    std::thread consume1_thread(::consume, std::move(consumer1), \"Consumer1\");\n    std::thread consume2_thread(::consume, std::move(consumer2), \"Consumer2\");\n\n    produce1_thread.join();\n    produce2_thread.join();\n    consume1_thread.join();\n    consume2_thread.join();\n\n    return 0;\n}\n```\n\n## Semantic Versioning\n\nVersioning of the library is done using conventional semantic versioning. Accordingly,\nin the versioning made as **MAJOR.MINOR.PATCH**;\n\n**PATCH:** Includes possible Bug\u0026Fixes and improvements. You definitely want to get this.\n\n**MINOR:** Additional functionality added via backwards compatibility. You probably want to\nget this, it doesn't hurt.\n\n**MAJOR:** Additional functionality that breaks backwards compatibility. You'll need to know\nwhat's changed before you get it, and you'll probably have to make changes to your own code.\nIf I publish something like this, I will definitely add the changes required for migration\nsection to the documentation.\n\n## License\n\nMIT License\n\nCopyright (c) 2024 Levent KARAGÖL\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n\n## Contact\n\nIf you have problems regarding the library, please open an\n[issue on GitHub](https://github.com/leventkaragol/libcpp-channel/issues/new).\nPlease describe your request, issue, or question in as much detail as possible\nand also include the version of your compiler and operating system, as well as\nthe version of the library you are using. Before opening a new issue, please\nconfirm that the topic is not already exists in closed issues.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleventkaragol%2Flibcpp-channel","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fleventkaragol%2Flibcpp-channel","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleventkaragol%2Flibcpp-channel/lists"}