#include <thread_pool.hpp>
template<typename JOB, typename FUNC>
thread_pool class
a generic thread pool that continuously waits for jobs from the job queue and processes them
| Template parameters | |
|---|---|
| JOB | the type of JOB that is transferred from the producer to thread pool |
| FUNC | the type of function that will be used to handle the jobs |
Example
#include <thread_pool.hpp> // recommended to declare a job type and a job func type using job_t = int; using job_func_t = std::function<void(int)>; // create a matching job handler std::atomic<int> count; void is_prime(int x) { if (x <= 1) { return; } for (int i = 2; i * i <= x; ++i) { if (x % i == 0) { return; } } ++count; } // create a thread pool mt::thread_pool<job_t, job_func_t> pool(1000, is_prime); // keep feeding it more job int k = 0; while (1) { pool.submit_job(k++); }
Public static variables
- static size_t DEF_NUM_THREADS constexpr
Constructors, destructors, conversion operators
- thread_pool(size_t cap, FUNC func)
- Construct a new thread pool object.
- ~thread_pool()
- Destroy the thread pool object.
-
thread_pool(const thread_
pool& pool) deleted - Copy constructor is deleted.
-
thread_pool(thread_
pool&& pool) deleted - Move constructor is deleted.
Public functions
- auto submit_job(const JOB& job) -> bool -> auto
- Submit a new job to the thread pool.
- auto terminate() -> bool -> auto
- Terminate the thread pool.
-
auto operator=(const thread_
pool& pool) -> thread_pool & -> auto deleted - copy assignment operator is deleted
-
auto operator=(thread_
pool&& pool) -> thread_pool & -> auto deleted - move assignment operator is deleted
Function documentation
template<typename JOB, typename FUNC>
mt:: thread_pool<JOB, FUNC>:: thread_pool(size_t cap,
FUNC func)
Construct a new thread pool object.
| Parameters | |
|---|---|
| cap | capacity of the underlying job queue |
| func | function handler that will be used to process the jobs |
template<typename JOB, typename FUNC>
auto mt:: thread_pool<JOB, FUNC>:: submit_job(const JOB& job) -> bool
Submit a new job to the thread pool.
| Parameters | |
|---|---|
| job | job to be submitted |
| Returns | true if job was successfully submitted |
template<typename JOB, typename FUNC>
auto mt:: thread_pool<JOB, FUNC>:: terminate() -> bool
Terminate the thread pool.
| Returns | true if successfully terminated the pool |
|---|