{"id":13419171,"url":"https://github.com/nbsdx/ThreadPool","last_synced_at":"2025-03-15T04:32:06.500Z","repository":{"id":78877049,"uuid":"45747235","full_name":"nbsdx/ThreadPool","owner":"nbsdx","description":"Lightweight, Generic, Pure C++11 ThreadPool","archived":true,"fork":false,"pushed_at":"2017-12-12T03:55:15.000Z","size":13,"stargazers_count":271,"open_issues_count":3,"forks_count":54,"subscribers_count":33,"default_branch":"master","last_synced_at":"2024-07-31T22:46:07.348Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/nbsdx.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":"2015-11-07T17:45:19.000Z","updated_at":"2024-07-30T12:16:00.000Z","dependencies_parsed_at":null,"dependency_job_id":"a2ef0411-edd7-45da-a58f-eb7dec0277d8","html_url":"https://github.com/nbsdx/ThreadPool","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/nbsdx%2FThreadPool","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nbsdx%2FThreadPool/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nbsdx%2FThreadPool/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nbsdx%2FThreadPool/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nbsdx","download_url":"https://codeload.github.com/nbsdx/ThreadPool/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243685506,"owners_count":20330980,"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-07-30T22:01:12.272Z","updated_at":"2025-03-15T04:32:06.495Z","avatar_url":"https://github.com/nbsdx.png","language":"C++","readme":"# ThreadPool\nLightweight, Generic, Pure C++11 ThreadPool\n\n## Rational\nI needed a Thread Pool for something I was writing, and I didn't see any that I liked. This is still somewhat a work in progress; it's stable, but there are probably places where some of the locking logic could be better. `ThreadPool::JoinAll` is a little sloppy, but it works. \n\n## Licensing \nPublic Domain. If my licensing is wrong, please let me know. Use at your own risk for whatever you want. Feel free to change the namespaces as well. Apparently licensing is hard and complicated. If your country doesn't have a public domain, feel free to say you found this on the side of the road. \n\n## Overview\n`ThreadPool` is a super simple class that manages threads and jobs. `ThreadCount` threads are created at object instantiation time, and persist until the `ThreadPool` object is destroyed. You cannot change the thread count. A later version may allow you to set the thread count through the constructor rather than as a template parameter, but it's not something I care to do at the moment. Jobs are functions with no parameters or return values. This decision was to make it as generic as possible so it could be integrated into a variety of projects. If you can't get your job to work with those constraints, you're doing something wrong, or you need to roll your own ThreadPool. But you're probably making things overly complicated.\n\nBelow is a quick overview, but ThreadPool.h is documented, so just read that. It's less than 200 lines with comments.\n\n```c++\ntemplate \u003cunsigned ThreadCount = 10\u003e\nclass ThreadPool {\npublic:\n    ThreadPool();\n    ~ThreadPool();\n    void AddJob( std::function\u003cvoid(void)\u003e );\n    unsigned Size() const;\n    unsigned JobsRemaining();\n    void JoinAll( bool WaitForAll = true );\n    void WaitAll();\n};\n```\n\n## Examples\n```c++\n#include \"ThreadPool.h\"\n\n#include \u003ciostream\u003e\n#include \u003cchrono\u003e\n\nint main() {\n    using nbsdx::concurrent::ThreadPool;\n    \n    ThreadPool pool; // Defaults to 10 threads.\n    int JOB_COUNT = 100;\n    \n    for( int i = 0; i \u003c JOB_COUNT; ++i )\n        pool.AddJob( []() { \n            std::this_thread::sleep_for( std::chrono::seconds( 1 ) );\n        } );\n    \n    pool.JoinAll();\n    std::cout \u003c\u003c \"Expected runtime: 10 seconds.\" \u003c\u003c std::endl;\n}\n```\n\nConvience Function for running a list of jobs in a pool, assuming the type being iterated is of `std::function\u003cvoid(void)\u003e`:\n```c++\ntemplate \u003ctypename Iter, unsigned Count = 10\u003e\nvoid RunInPool( Iter begin, Iter end ) {\n    ThreadPool\u003cCount\u003e pool;\n    for( ; begin != end; begin = std::next( begin ) )\n        pool.AddJob( *begin );\n    pool.JoinAll();\n}\n```\nIt's worth nothing that the `pool.JoinAll();` is optional in this example, since `JoinAll` is invoked upon object deconstruction. \n","funding_links":[],"categories":["TODO scan for Android support in followings","C++"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnbsdx%2FThreadPool","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnbsdx%2FThreadPool","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnbsdx%2FThreadPool/lists"}