{"id":15047858,"url":"https://github.com/sebsikora/void_thread_pool","last_synced_at":"2026-03-16T20:02:45.905Z","repository":{"id":201446297,"uuid":"425024157","full_name":"sebsikora/void_thread_pool","owner":"sebsikora","description":"An ultra-simple thread pool implementation for running void() functions in multiple worker threads","archived":false,"fork":false,"pushed_at":"2021-11-12T22:10:04.000Z","size":51,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-20T17:17:23.784Z","etag":null,"topics":["concurrency","cplusplus-11","cpp","multithreading","thread-pool","threading","threadpool","worker-threads"],"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/sebsikora.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}},"created_at":"2021-11-05T17:00:56.000Z","updated_at":"2021-11-19T12:59:10.000Z","dependencies_parsed_at":null,"dependency_job_id":"fb5d1072-47a6-4883-b5fb-9012d06542a9","html_url":"https://github.com/sebsikora/void_thread_pool","commit_stats":null,"previous_names":["sebsikora/void_thread_pool"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebsikora%2Fvoid_thread_pool","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebsikora%2Fvoid_thread_pool/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebsikora%2Fvoid_thread_pool/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebsikora%2Fvoid_thread_pool/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sebsikora","download_url":"https://codeload.github.com/sebsikora/void_thread_pool/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243489778,"owners_count":20298997,"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":["concurrency","cplusplus-11","cpp","multithreading","thread-pool","threading","threadpool","worker-threads"],"created_at":"2024-09-24T21:05:21.015Z","updated_at":"2025-12-28T20:15:33.613Z","avatar_url":"https://github.com/sebsikora.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# void_thread_pool.cpp\n\n© 2021 Dr Sebastien Sikora.\n\n[seb.nf.sikora@protonmail.com](mailto:seb.nf.sikora@protonmail.com)\n\nUpdated 06/11/2021.\n\nWhat is it?\n-------------------------\nvoid_thread_pool.cpp is an ultra-simple thread pool implementation for running functions that return `void` in multiple worker threads.\n\nIt is a synthesis of the brilliantly helpful code snippets and examples by users [phd-ap-ece](https://stackoverflow.com/users/3818417/phd-ap-ece) and [pio](https://stackoverflow.com/users/2724420/pio) [here](https://stackoverflow.com/questions/15752659/thread-pooling-in-c11).\nUseful supplementary information found [here](https://stackoverflow.com/questions/10673585/start-thread-with-member-function).\nExplanation of the syntax `[this]() { ... }` for the predicate second argument to `std::condition_variable.wait()` found [here](https://stackoverflow.com/questions/39565218/c-condition-variable-wait-for-predicate-in-my-class-stdthread-unresolved-o).\n\nHow to use it?\n-------------------------\nSay we have a function, or a class with a member function that returns `void`, that we would like to call in it's own thread.\n```cpp\nvoid FooBar1() {\n\tstd::string message = \"FooBar1 call.\\n\";\n\tstd::cout \u003c\u003c message;\n}\n\nvoid FooBar2(int id) {\n\tstd::string message = \"My id is \" + std::to_string(id) + \"\\n\";\n\tstd::cout \u003c\u003c message;\n}\n\nclass Foo {\n\tprivate:\n\t\tint m_id = 0;\n\tpublic:\n\t\tFoo(int id) { m_id = id; }\n\t\t~Foo() { }\n\t\tvoid Bar(void) { *** Stuff happens *** }\n};\n```\nFirst we instantiate the thread pool.\n```cpp\nVoidThreadPool thread_pool(true);\n\n// Optional first argument set to true prints pool messages to the console.\n\nVoidThreadPool thread_pool(true, 8)\n\n// If we don't specify the optional second number_of_workers argument it defaults to the\n// thread count returned by std::thread::hardware_concurrency().\n```\nNow, to queue a job for solution, we add it to the thread pool job queue using the thread pool's `AddJob()` member function.\n```cpp\nthread_pool.AddJob(FooBar1);\nthread_pool.AddJob(\u0026FooBar1);\n\n// We can pass the function by reference to avoid making a copy.\n\nthread_pool.AddJob(std::bind(\u0026FooBar2, i));\n\n// If we need to pass arguments with the function we encapsulate both using std::bind.\n\nFoo a_foo;\n\nthread_pool.AddJob(std::bind(\u0026Foo::Bar, a_foo));\n\n// If we want to pass a class member function, we again use std::bind to encapsulate a\n// pointer to a member function of the class, followed by an instance of that class.\n\nthread_pool.AddJob(std::bind(\u0026Foo::Bar, \u0026a_foo));\n\n// We can pass the class instance by reference, otherwise std::bind will make a copy of the\n// object. However, we are then taking responsibility for making sure the referenced object\n// still exists for the duration of the thread's execution.\n```\nWorker threads will begin to execute jobs as soon as they are added to the queue. If we need to wait for all jobs added to the queue to be completed prior to doing something else, we call the thread pool's `WaitForAllJobs()` member function. This will return when the job queue is empty and all running jobs are completed.\n\n\nTo stop the thread pool, we call it's `Finish()` method, which will stop the worker threads once all currently queued jobs are completed. `delete`ing a VoidThreadPool via a pointer to call it's destructor will first call `Finish()`.  \n\n\nFor a complete usage example [see](https://github.com/sebsikora/void_thread_pool/blob/main/demo.cpp) `demo.cpp`.\n\n\nTo compile the example (for example, using gcc), enter `g++ -pthread -o main -g void_thread_pool.cpp demo.cpp` on the command line. Note the `-pthread` compiler flag to add multi-threading support via the pthreads library.\n\nLicense:\n-------------------------\n![Mit License Logo](./220px-MIT_logo.png)\n\u003cbr/\u003e\u003cbr/\u003e\nvoid_thread_pool.cpp is distributed under the terms of the MIT license.\nLearn about the MIT license [here](https://choosealicense.com/licenses/mit/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsebsikora%2Fvoid_thread_pool","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsebsikora%2Fvoid_thread_pool","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsebsikora%2Fvoid_thread_pool/lists"}