{"id":15047750,"url":"https://github.com/developerpaul123/thread-pool","last_synced_at":"2025-05-15T18:10:39.863Z","repository":{"id":40329964,"uuid":"369599975","full_name":"DeveloperPaul123/thread-pool","owner":"DeveloperPaul123","description":"A modern, fast, lightweight thread pool library based on C++20","archived":false,"fork":false,"pushed_at":"2025-01-01T15:10:37.000Z","size":739,"stargazers_count":496,"open_issues_count":12,"forks_count":41,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-05-08T10:46:15.886Z","etag":null,"topics":["c-plus-plus","concurrency","cplusplus","cpp","cpp20","cpp20-library","fast","header-only","high-performance","high-performance-computing","modern-cpp","performance","thread","thread-pool","thread-pool-implementations","threading","threadpool","threads"],"latest_commit_sha":null,"homepage":"https://DeveloperPaul123.github.io/thread-pool","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/DeveloperPaul123.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","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},"funding":{"github":"DeveloperPaul123"}},"created_at":"2021-05-21T16:52:38.000Z","updated_at":"2025-04-29T19:23:00.000Z","dependencies_parsed_at":"2024-05-03T16:12:20.479Z","dependency_job_id":"2d28eeb0-fbda-4b1d-aae5-adb97e829370","html_url":"https://github.com/DeveloperPaul123/thread-pool","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":"TheLartians/ModernCppStarter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DeveloperPaul123%2Fthread-pool","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DeveloperPaul123%2Fthread-pool/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DeveloperPaul123%2Fthread-pool/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DeveloperPaul123%2Fthread-pool/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DeveloperPaul123","download_url":"https://codeload.github.com/DeveloperPaul123/thread-pool/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254394724,"owners_count":22063984,"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":["c-plus-plus","concurrency","cplusplus","cpp","cpp20","cpp20-library","fast","header-only","high-performance","high-performance-computing","modern-cpp","performance","thread","thread-pool","thread-pool-implementations","threading","threadpool","threads"],"created_at":"2024-09-24T21:04:10.503Z","updated_at":"2025-05-15T18:10:39.827Z","avatar_url":"https://github.com/DeveloperPaul123.png","language":"C++","readme":"\u003ch1 align=center\u003e\nthread-pool\n\u003c/h1\u003e\n\n[![say thanks](https://img.shields.io/badge/Say%20Thanks-👍-1EAEDB.svg)](https://github.com/DeveloperPaul123/thread-pool/stargazers)\n[![Discord](https://img.shields.io/discord/652515194572111872?logo=Discord)](https://discord.gg/CX2ybByRnt)\n![License](https://img.shields.io/github/license/DeveloperPaul123/thread-pool?color=blue)\n![Release](https://img.shields.io/github/v/release/DeveloperPaul123/thread-pool)\n![Documentation](https://img.shields.io/website?label=docs\u0026url=https%3A%2F%2Fdeveloperpaul123.github.io%2Fthread-pool%2F)\n\n[![Ubuntu](https://github.com/DeveloperPaul123/thread-pool/actions/workflows/ubuntu.yml/badge.svg)](https://github.com/DeveloperPaul123/thread-pool/actions/workflows/ubuntu.yml)\n[![Windows](https://github.com/DeveloperPaul123/thread-pool/actions/workflows/windows.yml/badge.svg)](https://github.com/DeveloperPaul123/thread-pool/actions/workflows/windows.yml)\n[![Style](https://github.com/DeveloperPaul123/thread-pool/actions/workflows/style.yml/badge.svg)](https://github.com/DeveloperPaul123/thread-pool/actions/workflows/style.yml)\n[![Install](https://github.com/DeveloperPaul123/thread-pool/actions/workflows/install.yml/badge.svg)](https://github.com/DeveloperPaul123/thread-pool/actions/workflows/install.yml)\n[![codecov](https://codecov.io/github/DeveloperPaul123/thread-pool/branch/feature/codecov/graph/badge.svg?token=Y47HDBRVR3)](https://codecov.io/github/DeveloperPaul123/thread-pool)\n\nA simple, *fast* and functional thread pool implementation using pure C++20.\n\n## Features\n\n* Built entirely with C++20\n* Enqueue tasks with or without tracking results\n* [High performance](#benchmarks)\n\n## Integration\n\n`dp::thread-pool` is a header only library. All the files needed are in `include/thread_pool`.\n\n### vcpkg\n\n`dp::thread-pool` is available on `vcpkg`\n\n```powershell\nvcpkg install dp-thread-pool\n```\n\n### CMake\n\n`thread-pool` defines the CMake target `dp::thread-pool`.\n\nYou can then use `find_package()`:\n\n```cmake\nfind_package(dp::thread-pool REQUIRED)\n```\n\nAlternatively, you can use something like [CPM](https://github.com/TheLartians/CPM) which is based on CMake's `Fetch_Content` module.\n\n```cmake\nCPMAddPackage(\n  NAME thread-pool\n  GITHUB_REPOSITORY DeveloperPaul123/thread-pool\n  GIT_TAG 0.6.0 # change this to latest commit or release tag\n  OPTIONS\n    \"TP_BUILD_TESTS OFF\"\n    \"TP_BUILD_BENCHMARKS OFF\"\n    \"TP_BUILD_EXAMPLES OFF\"  \n)\n```\n\n## Usage\n\nEnqueue tasks without a returned result:\n\n```cpp\n// create a thread pool with a specified number of threads.\ndp::thread_pool pool(4);\n\n// add tasks, in this case without caring about results of individual tasks\npool.enqueue_detach([](int value) { /*...your task...*/ }, 34);\npool.enqueue_detach([](int value) { /*...your task...*/ }, 37);\npool.enqueue_detach([](int value) { /*...your task...*/ }, 38);\n// and so on..\n```\n\nEnqueue tasks with a returned value:\n\n```cpp\n// create a thread pool with a specified number of threads.\ndp::thread_pool pool(4);\n\nauto result = pool.enqueue([](int value) -\u003e int { /*...your task...*/ return value; }, 34);\n// get the result, this will block the current thread until the task is complete\nauto value = result.get();\n```\n\nEnqueue tasks and wait for them to complete:\n\n```cpp\ndp::thread_pool pool(4);\n\n// add tasks, in this case without caring about results of individual tasks\npool.enqueue_detach([](int value) { /*...your task...*/ }, 34);\npool.enqueue_detach([](int value) { /*...your task...*/ }, 37);\npool.enqueue_detach([](int value) { /*...your task...*/ }, 38);\npool.enqueue_detach([](int value) { /*...your task...*/ }, 40);\n\n// wait for all tasks to complete\npool.wait_for_tasks();\n```\n\nYou can see other examples in the `/examples` folder.\n\n## Benchmarks\n\nBenchmarks were run using the [nanobench](https://github.com/martinus/nanobench) library. See the `./benchmark` folder for the benchmark code. The benchmarks are set up to compare matrix multiplication using the `dp::thread_pool` versus other thread pool libraries. These include:\n\n* [ConorWilliams/Threadpool](https://github.com/ConorWilliams/Threadpool)\n* [bshoshany/thread-pool](https://github.com/bshoshany/thread-pool) (C++17)\n* [alugowski/task-thread-pool](https://github.com/alugowski/task-thread-pool) (C++11, no work stealing)\n\nThe benchmarks are set up so that each library is tested against `dp::thread_pool` using `std::function` as the baseline. Relative measurements (in %) are recorded to compare the performance of each library to the baseline.\n\n### Machine Specs\n\n* AMD Ryzen 7 5800X (16 X 3800 MHz CPUs)\n* 32 GB RAM\n\n### Results\n\n#### Summary\n\nIn general, `dp::thread_pool` is faster than other thread pool libraries in most cases. This is especially the case when `std::move_only_function` is available. `fu2::unique_function` is a close second, and `std::function` is the sloweset when used in `dp::thread_pool`. In certain situations, `riften::ThreadPool` pulls ahead in performance. This is likely due to the fact that this library uses a lock-free queue. There is also a custom semaphore and it seems that there is a difference in how work stealing is handled as well. Interestingly, `task_thread_pool` seems to pull ahead with large numbers of smaller tasks.\n\n#### Details\n\nBelow is a portion of the benchmark data from the MSVC results:\n\n| relative |               ms/op |                op/s |    err% |     total | matrix multiplication 256x256\n|---------:|--------------------:|--------------------:|--------:|----------:|:------------------------------\n|   100.0% |               93.27 |               10.72 |    0.7% |     16.69 | `dp::thread_pool - std::function`\n|   102.9% |               90.66 |               11.03 |    0.6% |     16.22 | `dp::thread_pool - std::move_only_function`\n|    98.7% |               94.50 |               10.58 |    0.2% |     16.91 | `dp::thread_pool - fu2::unique_function`\n|    93.5% |               99.73 |               10.03 |    0.4% |     17.86 | `BS::thread_pool`\n|   102.2% |               91.29 |               10.95 |    0.6% |     16.39 | `task_thread_pool`\n|   100.1% |               93.18 |               10.73 |    1.4% |     16.61 | `riften::Thiefpool`\n\nIf you wish to look at the full results, use the links below.\n\n[MSVC Results](./benchmark/results/benchmark_results_msvc.md)\n\n[Clang Results](./benchmark/results/benchmark_results_clang.md)\n\nSome notes on the benchmark methodology:\n\n* Matrix sizes are all square (MxM).\n* Each multiplication is `(MxM) * (MxM)` where `*` refers to a matrix multiplication operation.\n* Benchmarks were run on Windows, so system stability is something to consider (dynamic CPU frequency scaling, etc.).\n* Relative\n\n## Building\n\nThis project has been built with:\n\n* Visual Studio 2022\n* Clang `10.+` (via WSL on Windows)\n* GCC `11.+` (vis WSL on Windows)\n* CMake `3.19+`\n\nTo build, run:\n\n```bash\ncmake -S . -B build\ncmake --build build\n```\n\n### Build Options\n\n| Option              | Description                                                         | Default |\n|:--------------------|:--------------------------------------------------------------------|:-------:|\n| `TP_BUILD_TESTS`    | Turn on to build unit tests. Required for formatting build targets. |   ON    |\n| `TP_BUILD_EXAMPLES` | Turn on to build examples                                           |   ON    |\n\n### Run clang-format\n\nUse the following commands from the project's root directory to check and fix C++ and CMake source style.\nThis requires *clang-format*, *cmake-format* and *pyyaml* to be installed on the current system. To use this feature you must turn on `TP_BUILD_TESTS`.\n\n```bash\n# view changes\ncmake --build build/test --target format\n\n# apply changes\ncmake --build build/test --target fix-format\n```\n\nSee [Format.cmake](https://github.com/TheLartians/Format.cmake) for details.\n\n### Build the documentation\n\nThe documentation is automatically built and [published](https://developerpaul123.github.io/thread-pool) whenever a [GitHub Release](https://help.github.com/en/github/administering-a-repository/managing-releases-in-a-repository) is created.\nTo manually build documentation, call the following command.\n\n```bash\ncmake -S documentation -B build/doc\ncmake --build build/doc --target GenerateDocs\n# view the docs\nopen build/doc/doxygen/html/index.html\n```\n\nTo build the documentation locally, you will need Doxygen and Graphviz on your system.\n\n## Contributing\n\nContributions are very welcome. Please see [contribution guidelines for more info](CONTRIBUTING.md).\n\n## License\n\nThe project is licensed under the MIT license. See [LICENSE](LICENSE) for more details.\n\n## Author\n\n| [\u003cimg src=\"https://avatars0.githubusercontent.com/u/6591180?s=460\u0026v=4\" width=\"100\"\u003e\u003cbr\u003e\u003csub\u003e@DeveloperPaul123\u003c/sub\u003e](https://github.com/DeveloperPaul123) |\n|:----:|\n","funding_links":["https://github.com/sponsors/DeveloperPaul123"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeveloperpaul123%2Fthread-pool","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdeveloperpaul123%2Fthread-pool","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeveloperpaul123%2Fthread-pool/lists"}