{"id":13730026,"url":"https://github.com/Tyler-Hardin/thread_pool","last_synced_at":"2025-05-08T02:31:04.312Z","repository":{"id":17905340,"uuid":"20862344","full_name":"Tyler-Hardin/thread_pool","owner":"Tyler-Hardin","description":"Thread pool using std::* primitives from C++20, with optional priority queue/greenthreading for POSIX.","archived":false,"fork":false,"pushed_at":"2022-08-29T15:57:05.000Z","size":26,"stargazers_count":82,"open_issues_count":0,"forks_count":13,"subscribers_count":10,"default_branch":"master","last_synced_at":"2024-11-14T20:38:55.420Z","etag":null,"topics":["c-plus-plus-17","c-plus-plus-20","concepts","concurrency","cpp","priority-pool","thread-pool"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Tyler-Hardin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-06-15T18:37:27.000Z","updated_at":"2024-07-07T15:25:46.000Z","dependencies_parsed_at":"2023-01-11T19:43:33.539Z","dependency_job_id":null,"html_url":"https://github.com/Tyler-Hardin/thread_pool","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/Tyler-Hardin%2Fthread_pool","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tyler-Hardin%2Fthread_pool/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tyler-Hardin%2Fthread_pool/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tyler-Hardin%2Fthread_pool/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Tyler-Hardin","download_url":"https://codeload.github.com/Tyler-Hardin/thread_pool/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252986681,"owners_count":21836205,"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-17","c-plus-plus-20","concepts","concurrency","cpp","priority-pool","thread-pool"],"created_at":"2024-08-03T02:01:08.886Z","updated_at":"2025-05-08T02:31:04.081Z","avatar_url":"https://github.com/Tyler-Hardin.png","language":"C++","readme":"thread_pool\n===========\n\nSimple thread pool using only standard library components. Also includes a class for a priority thread pool.\n\nRequires concepts and C++20. Currently only GCC 10.0+ is sufficient. Use `-std=c++20 -fconcepts` to compile.\n\nThe priority thread pool is only supported on POSIX/-like systems. But it's still easy to use the normal pool on non-POSIX; just don't compile priority_thread_pool.cpp or include the header.\n\nFor just C++11, use `8bdfb9b`. `5ea01d0` was the latest to support \u003c= C++14. For C++17, use `e3be25` and compile with `-std=c++17 -fconcepts`.\n\nThe priority pool has the same API as described below, accept it has an int parameter first for the priority of the task. E.g. `pool.async(5, func, arg1, arg2)` for priority 5.\n\nAn example that computes the primality of 2 to 10,000 using 8 threads:\n```c++\n#include \"thread_pool.hpp\"\n\n#include \u003ciostream\u003e\n#include \u003clist\u003e\n#include \u003cutility\u003e\n\nusing namespace std;\n\n// Return the integer argument and a boolean representing its primality.\n// We need to return the integer because the loop that retrieves results doesn't know which integer corresponds to which future.\npair\u003cint, bool\u003e is_prime(int n){\n  for(int i = 2;i \u003c n;i++)\n    if(n % i == 0)\n      return make_pair(i, false);\n  return make_pair(n, true);\n}\n\nint main(){\n  thread_pool pool(8);                   // Contruct a thread pool with 8 threads.\n  list\u003cfuture\u003cpair\u003cint, bool\u003e\u003e\u003e results;\n  for(int i = 2;i \u003c 10000;i++){\n  \t// Add a task to the queue.\n    results.push_back(pool.async(is_prime, i));\n  }\n  \n  for(auto i = results.begin();i != results.end();i++){\n    pair\u003cint, bool\u003e result = i-\u003eget();  // Get the pair\u003cint, bool\u003e from the future\u003c...\u003e\n    cout \u003c\u003c result.first \u003c\u003c \": \" \u003c\u003c (result.second ? \"is prime\" : \"is composite\") \u003c\u003c endl;\n  }\n  return 0;\n}\n```\n\n`thread_pool::async` is a templated method that accepts any `std::function` and arguments to pass to the function. It returns a `std::future\u003cRet\u003e` where `Ret` is the return type of the aforementioned `std::function`.\n\nTo submit a task: `future\u003cRet\u003e fut(pool.async(func, args...));`.\n\nTo wait on `fut` to complete: `Ret result = fut.get();`\n","funding_links":[],"categories":["C++"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTyler-Hardin%2Fthread_pool","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FTyler-Hardin%2Fthread_pool","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTyler-Hardin%2Fthread_pool/lists"}