{"id":13729977,"url":"https://github.com/revsic/cpp-concurrency","last_synced_at":"2025-05-05T17:33:37.114Z","repository":{"id":140177579,"uuid":"157092491","full_name":"revsic/cpp-concurrency","owner":"revsic","description":"cpp implementation of golang style concurrency","archived":false,"fork":false,"pushed_at":"2019-06-02T08:41:46.000Z","size":163,"stargazers_count":15,"open_issues_count":4,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-05T17:32:18.991Z","etag":null,"topics":[],"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/revsic.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}},"created_at":"2018-11-11T15:21:32.000Z","updated_at":"2024-05-23T14:10:48.000Z","dependencies_parsed_at":"2024-01-31T00:20:39.741Z","dependency_job_id":"74adc46b-8930-4f98-bcde-18b65a77fbf5","html_url":"https://github.com/revsic/cpp-concurrency","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/revsic%2Fcpp-concurrency","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/revsic%2Fcpp-concurrency/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/revsic%2Fcpp-concurrency/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/revsic%2Fcpp-concurrency/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/revsic","download_url":"https://codeload.github.com/revsic/cpp-concurrency/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252542526,"owners_count":21764984,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":[],"created_at":"2024-08-03T02:01:08.341Z","updated_at":"2025-05-05T17:33:37.083Z","avatar_url":"https://github.com/revsic.png","language":"C++","readme":"# cpp-concurrency\n\n[![License](https://img.shields.io/badge/Licence-MIT-blue.svg)](https://github.com/revsic/cpp-concurrency/blob/master/LICENSE)\n[![Build Status](https://dev.azure.com/revsic99/cpp-concurrency/_apis/build/status/revsic.cpp-concurrency?branchName=master)](https://dev.azure.com/revsic99/cpp-concurrency/_build/latest?definitionId=2?branchName=master)\n\nC++ implementation of golang style concurrency\n\n## Usage\n\nUse existing single header [concurrency.hpp](./concurrency.hpp) or run [script](./script/merge.py) to merge multiple headers in directory [concurrency](./concurrency).\n```\npython -m script.merge\n```\n\nInclude [concurrency.hpp](./concurrency.hpp) to use it.\n```C++\n#include \"concurrency.hpp\"\n\nThreadPool\u003cint\u003e pool;\nLChannel\u003cint\u003e channel;\nWaitGroup wg;\n```\n\n## Sample\n\nSend tick and bomb\n```\ng++ -o tick ./sample/tick.cpp -std=c++17 -lpthread #linux\ncl /EHsc /std:c++17 ./sample/tick.cpp              #windows\n```\n\nCalculate directory size concurrently.\n```\ng++ -o dir_size ./sample/dir_size.cpp -std=c++17 -lstdc++fs -lpthread\ncl /EHsc /std:c++17 ./sample/dir_size.cpp\n```\n\n## Channel\n\n- RChannel\u003cT\u003e : finite capacity channel, if capacity exhausted, block channel and wait for space.\n- LChannel\u003cT\u003e : list like channel.\n\nAdd and get from channel.\n```C++\nRChannel\u003cstd::string\u003e channel(3);\nchannel.Add(\"test\");\n\nstd::optional\u003cstd::string\u003e res = channel.Get();\nassert(res.value() == \"test\");\n```\n\nWith stream operator\n```C++\nchannel \u003c\u003c \"test\";\n\nstd::string res;\nchannel \u003e\u003e res;\n```\n\nGolang style channel range iteration.\n```C++\nLChannel\u003cint\u003e channel;\nauto fut = std::async(std::launch::async, [\u0026]{ \n    for (int i = 0; i \u003c 3; ++i) {\n        channel \u003c\u003c i;\n    }\n    channel.Close();\n});\n\nfor (auto const\u0026 elem : channel) {\n    std::cout \u003c\u003c elem \u003c\u003c ' ';\n}\nstd::cout \u003c\u003c std::endl;\n```\n\n## Thread Pool\n\n- ThreadPool\u003cT\u003e : finite task thread pool, if capacity exhausted, block new and wait for existing tasks to be terminated.\n- LThreadPool\u003cT\u003e : list based thread pool.\n\nAdd new tasks and get return value from future.\n```C++\nThreadPool\u003cint\u003e pool;\nstd::future\u003cint\u003e fut = pool.Add([]{\n    int sum = 0;\n    for (int i = 0; i \u003c 5; ++i) {\n        sum += i;\n    }\n    return sum;\n});\n\nassert(fut.get() == 1 + 2 + 3 + 4);\n```\n\n## Wait Group\n\nWait until all visits are done.\n\n```C++\nWaitGroup wg;\nwg.Add();\n\nauto fut = std::async(std::launch::async, [\u0026]{\n    std::this_thread::sleep_for(2s);\n    wg.Done();\n});\n\nauto start = std::chrono::steady_clock::now();\nwg.Wait();\nauto end = std::chrono::steady_clock::now();\n\nstd::cout \u003c\u003c std::chrono::duration_cast\u003cstd::chrono::seconds\u003e(end - start).count();\n```\n\n## Select\n\nChannel operation multiplexer, samples from [tick.cpp](./sample/tick.cpp)\n```C++\nauto tick = Tick(100ms);\nauto boom = After(500ms);\n\nbool cont = true;\nwhile (cont) {\n    select(\n        case_m(*tick) \u003e\u003e []{ \n            std::cout \u003c\u003c \"tick.\" \u003c\u003c std::endl; \n        },\n        case_m(*boom) \u003e\u003e [\u0026]{ \n            std::cout \u003c\u003c \"boom !\" \u003c\u003c std::endl;\n            cont = false; \n        },\n        default_m \u003e\u003e [\u0026]{\n            std::cout \u003c\u003c \".\" \u003c\u003c std::endl;\n            sleep(50ms);\n        }\n    );\n}\ntick-\u003eClose();\n```\n","funding_links":[],"categories":["C++"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frevsic%2Fcpp-concurrency","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frevsic%2Fcpp-concurrency","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frevsic%2Fcpp-concurrency/lists"}