{"id":25658447,"url":"https://github.com/gilzoide/cpp-dispatch-queue","last_synced_at":"2026-06-18T11:31:59.744Z","repository":{"id":277262494,"uuid":"931860622","full_name":"gilzoide/cpp-dispatch-queue","owner":"gilzoide","description":"Dispatch Queue / Thread Pool implementation for C++11 with built-in C++20 coroutine support","archived":false,"fork":false,"pushed_at":"2026-05-23T02:24:21.000Z","size":295,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-05-23T04:19:42.408Z","etag":null,"topics":["concurrency","concurrent","coroutine","coroutines","cpp","cpp11","cpp20-coroutine","dispatch-queues","dispatchqueue","thread-pool","threading","threadpool"],"latest_commit_sha":null,"homepage":"https://gilzoide.github.io/cpp-dispatch-queue/","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gilzoide.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":null,"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},"funding":{"github":["gilzoide"],"patreon":null,"open_collective":null,"ko_fi":"gilzoide","tidelift":null,"community_bridge":null,"liberapay":"gilzoide","issuehunt":null,"lfx_crowdfunding":null,"polar":null,"buy_me_a_coffee":null,"thanks_dev":null,"custom":null}},"created_at":"2025-02-13T01:11:25.000Z","updated_at":"2026-05-23T02:24:24.000Z","dependencies_parsed_at":"2025-02-13T02:27:56.178Z","dependency_job_id":"65f9457d-fb98-458a-b0f2-d1e36b6d2f41","html_url":"https://github.com/gilzoide/cpp-dispatch-queue","commit_stats":null,"previous_names":["gilzoide/cpp-dispatch-queue"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/gilzoide/cpp-dispatch-queue","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gilzoide%2Fcpp-dispatch-queue","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gilzoide%2Fcpp-dispatch-queue/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gilzoide%2Fcpp-dispatch-queue/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gilzoide%2Fcpp-dispatch-queue/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gilzoide","download_url":"https://codeload.github.com/gilzoide/cpp-dispatch-queue/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gilzoide%2Fcpp-dispatch-queue/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34489036,"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-18T02:00:06.871Z","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":["concurrency","concurrent","coroutine","coroutines","cpp","cpp11","cpp20-coroutine","dispatch-queues","dispatchqueue","thread-pool","threading","threadpool"],"created_at":"2025-02-24T00:21:30.076Z","updated_at":"2026-06-18T11:31:59.737Z","avatar_url":"https://github.com/gilzoide.png","language":"C++","funding_links":["https://github.com/sponsors/gilzoide","https://ko-fi.com/gilzoide","https://liberapay.com/gilzoide"],"categories":[],"sub_categories":[],"readme":"# Dispatch Queue\nDispatch Queue / Thread Pool implementation for C++11 with built-in C++20 coroutine support.\n\n\n## Features\n- No external dependencies: uses only the C++ STL\n- Supports both immediate and threaded execution modes:\n  + Threaded dispatch queues are also known as Thread Pools.\n    In threaded mode it is safe to dispatch new tasks from any thread.\n  + In immediate mode tasks are executed immediately. Useful for multiplatform code that must work on platforms without thread support, for example WebAssembly on browsers that lack `SharedArrayBuffer` support.\n- Use `dispatch_queue.dispatch(f, args...)` to dispatch new tasks\n- Use `dispatch_queue.dispatch_main(f, args...)` to dispatch \"main loop\" tasks\n  + Users must call `dispatch_queue.main_loop()` manually where appropriate to run queued main loop tasks\n  + Useful for synchronizing state calculated in background tasks with the application's main loop\n- Returned `dispatch_queue::task\u003cT\u003e` from dispatch methods are similar to `std::shared_future`, with the following additions:\n  + Use `task.get_state()` to get whether task is pending, ready or failed with exception\n  + Use `task.then(f)` to add a continuation function that runs when task finishes\n  + Use `task.get_exception()` to get stored exception_ptr\n- Built-in C++20 coroutine support\n  + Use `dispatch_queue::task\u003cT\u003e` as the return value for your coroutines\n  + `co_await` other tasks to resume the coroutine as the task's continuation\n  + Use `co_await dispatch_queue.dispatch()` to continue coroutine in a dispatch queue's background loop\n  + Use `co_await dispatch_queue.dispatch_main()` to continue coroutine in a dispatch queue's main loop\n- Supports compiling with `-fno-exceptions` and `-fno-rtti`\n- Unified implementation file [src/dispatch_queue-one.cpp](src/dispatch_queue-one.cpp), easy to integrate in any project\n\n\n## Usage example\n```cpp\n#include \u003cdispatch_queue.hpp\u003e\n\n///////////////////////////////////////////////////////////\n// 1. Create a dispatch queue\n///////////////////////////////////////////////////////////\n\n// Default constructed dispatch queues are immediate.\n// They execute tasks immediately in the call to `dispatch`.\ndispatch_queue::dispatch_queue immediate_dispatcher;\n// Dispatch queues with 0 threads are also immediate.\ndispatch_queue::dispatch_queue immediate_dispatcher2(0);\n\n// A dispatch queue with 1 thread is a serial queue:\n// it runs a single task at a time in its background thread.\ndispatch_queue::dispatch_queue serial_dispatcher(1);\n\n// A dispatch queue with more than 1 thread runs tasks concurrently.\ndispatch_queue::dispatch_queue concurrent_dispatcher(4);\n\n// Pass a negative value to use the default thread count.\n// Current default is `std::thread::hardware_concurrency`.\ndispatch_queue::dispatch_queue concurrent_dispatcher2(-1);\n\n\n///////////////////////////////////////////////////////////\n// 2. Dispatch some tasks!\n///////////////////////////////////////////////////////////\n\n// Use the returned task to get results or wait for completion.\nauto work = []{ return 42; };\ndispatch_queue::task\u003cint\u003e task = dispatcher.dispatch(work);\nassert(task.get() == 42);\n\n// Pass arguments to forward to task\nauto work2 = [](int value) { return value; };\ndispatch_queue::task\u003cint\u003e task2 = dispatcher.dispatch(work2, 2);\nassert(task2.get() == 2);\n\n// Use `then` for adding continuations\ndispatch_queue::task\u003cvoid\u003e continued_task = dispatcher.dispatch(work)\n    // continuations receive the finished task\n    .then([](dispatch_queue::task\u003cint\u003e task) {\n        if (std::exception_ptr exception = task.get_exception()) {\n            // task failed with an exception...\n            std::rethrow_exception(exception);\n        }\n        else {\n            // task succeeded!\n            int result = task.get();\n            return (float) result;\n        }\n    })\n    // .then() return a new task, so you can chain continuations\n    .then([\u0026](dispatch_queue::task\u003cfloat\u003e task) {\n        return dispatcher.dispatch(work2);\n    })\n    // .then() unwraps task\u003ctask\u003cT\u003e\u003e if C++20 concepts are available\n    .then([](dispatch_queue::task\u003cint\u003e task) {\n        return;\n    });\ncontinued_task.wait();\n\n// Queue \"main loop\" tasks that will be executed by calling `main_loop()`\ndispatcher.dispatch_main([]{\n    std::cout \u003c\u003c \"This will run inside the call to `main_loop`\" \u003c\u003c std::endl;\n});\nwhile (!ApplicationShouldExit()) {\n    // Inside your application's main loop...\n    dispatcher.main_loop();\n}\n\n\n///////////////////////////////////////////////////////////\n// 3. Built-in C++20 coroutine support\n///////////////////////////////////////////////////////////\n\n// Use dispatch_queue::task\u003cT\u003e as return value for coroutines\ndispatch_queue::task\u003cvoid\u003e my_coro() {\n    // co_await other tasks\n    // coroutine becomes task's continuation via .then()\n    co_await dispatcher.dispatch(some_work);\n    do_something_after_some_work_finished();\n\n    // co_await .dispatch()\n    // coroutine continues within dispatch queue\n    co_await dispatcher.dispatch();\n    do_something_in_background();\n\n    // co_await .dispatch_main()\n    // coroutine continues within dispatch queue's main loop\n    co_await dispatcher.dispatch_main();\n    do_something_in_main_loop();\n}\n\n\n///////////////////////////////////////////////////////////\n// 4. Check some stats\n///////////////////////////////////////////////////////////\n\nint dispatcher_thread_count = dispatcher.thread_count();\nbool dispatcher_is_threaded = dispatcher.is_threaded();\nint pending_task_count = dispatcher.size();\nbool has_no_pending_tasks = dispatcher.empty();\n\n\n///////////////////////////////////////////////////////////\n// 5. Other operations\n///////////////////////////////////////////////////////////\n\n// Cancel all pending tasks.\n// Tasks already executing will still run to completion.\ndispatcher.clear();\n\n// Wait until pending tasks are completed\ndispatcher.wait();\n// Wait until pending tasks are completed, with timeout\ndispatcher.wait_for(std::chrono::seconds(5));\ndispatcher.wait_until(std::chrono::system_clock::now() + std::chrono::seconds(5));\n```\n\n\n## Using in CMake projects\nAdd this project using `add_subdirectory` and link your target to `dispatch_queue`:\n```cmake\nadd_subdirectory(\"path/to/dispatch_queue\")\ntarget_link_libraries(my_target dispatch_queue)\n```\n\n\n## Setting thread names for debugging\nYou can pass a functor to the dispatch queue constructor that will run inside worker threads when they initialize.\nThere you can set thread names:\n```cpp\ndispatch_queue::dispatch_queue dispatcher(4, [](int worker_index) {\n    std::string worker_name = std::format(\"worker{}\", worker_index);\n    // TODO: set thread name, platform-specific\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgilzoide%2Fcpp-dispatch-queue","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgilzoide%2Fcpp-dispatch-queue","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgilzoide%2Fcpp-dispatch-queue/lists"}