tlx
|
ThreadPool starts a fixed number p of std::threads which process Jobs that are enqueued into a concurrent job queue. More...
#include <thread_pool.hpp>
Public Types | |
using | Job = Delegate< void()> |
using | InitThread = Delegate< void(size_t)> |
Public Member Functions | |
ThreadPool (size_t num_threads=std::thread::hardware_concurrency(), InitThread &&init_thread=InitThread()) | |
Construct running thread pool of num_threads. More... | |
ThreadPool (const ThreadPool &)=delete | |
non-copyable: delete copy-constructor More... | |
ThreadPool & | operator= (const ThreadPool &)=delete |
non-copyable: delete assignment operator More... | |
~ThreadPool () | |
Stop processing jobs, terminate threads. More... | |
void | enqueue (Job &&job) |
enqueue a Job, the caller must pass in all context using captures. More... | |
void | loop_until_empty () |
Loop until no more jobs are in the queue AND all threads are idle. More... | |
void | loop_until_terminate () |
Loop until terminate flag was set. More... | |
void | terminate () |
Terminate thread pool gracefully, wait until currently running jobs finish and then exit. More... | |
size_t | done () const |
Return number of jobs currently completed. More... | |
size_t | size () const |
Return number of threads in pool. More... | |
size_t | idle () const |
return number of idle threads in pool More... | |
bool | has_idle () const |
true if any thread is idle (= waiting for jobs) More... | |
std::thread & | thread (size_t i) |
Return thread handle to thread i. More... | |
Private Member Functions | |
void | worker (size_t p) |
Worker function, one per thread is started. More... | |
Private Attributes | |
std::deque< Job > | jobs_ |
Deque of scheduled jobs. More... | |
std::mutex | mutex_ |
Mutex used to access the queue of scheduled jobs. More... | |
simple_vector< std::thread > | threads_ |
threads in pool More... | |
std::condition_variable | cv_jobs_ |
Condition variable used to notify that a new job has been inserted in the queue. More... | |
std::condition_variable | cv_finished_ |
Condition variable to signal when a jobs finishes. More... | |
std::atomic< size_t > | busy_ |
Counter for number of threads busy. More... | |
std::atomic< size_t > | idle_ |
Counter for number of idle threads waiting for a job. More... | |
std::atomic< size_t > | done_ |
Counter for total number of jobs executed. More... | |
std::atomic< bool > | terminate_ |
Flag whether to terminate. More... | |
InitThread | init_thread_ |
Run once per worker thread. More... | |
ThreadPool starts a fixed number p of std::threads which process Jobs that are enqueued into a concurrent job queue.
The jobs themselves can enqueue more jobs that will be processed when a thread is ready.
The ThreadPool can either run until
Jobs are plain tlx::Delegate<void()> objects, hence the pool user must pass in ALL CONTEXT himself. The best method to pass parameters to Jobs is to use lambda captures. Alternatively, old-school objects implementing operator(), or std::binds can be used.
The ThreadPool uses a condition variable to wait for new jobs and does not remain busy waiting.
Note that the threads in the pool start before the two loop functions are called. In case of loop_until_empty() the threads continue to be idle afterwards, and can be reused, until the ThreadPool is destroyed.
Definition at line 64 of file thread_pool.hpp.
using InitThread = Delegate<void (size_t)> |
Definition at line 68 of file thread_pool.hpp.
Definition at line 67 of file thread_pool.hpp.
ThreadPool | ( | size_t | num_threads = std::thread::hardware_concurrency() , |
InitThread && | init_thread = InitThread() |
||
) |
Construct running thread pool of num_threads.
Definition at line 17 of file thread_pool.cpp.
|
delete |
non-copyable: delete copy-constructor
~ThreadPool | ( | ) |
Stop processing jobs, terminate threads.
Definition at line 25 of file thread_pool.cpp.
size_t done | ( | ) | const |
Return number of jobs currently completed.
Definition at line 65 of file thread_pool.cpp.
void enqueue | ( | Job && | job | ) |
enqueue a Job, the caller must pass in all context using captures.
Definition at line 37 of file thread_pool.cpp.
bool has_idle | ( | ) | const |
true if any thread is idle (= waiting for jobs)
Definition at line 77 of file thread_pool.cpp.
size_t idle | ( | ) | const |
return number of idle threads in pool
Definition at line 73 of file thread_pool.cpp.
void loop_until_empty | ( | ) |
Loop until no more jobs are in the queue AND all threads are idle.
When this occurs, this method exits, however, the threads remain active.
Definition at line 43 of file thread_pool.cpp.
void loop_until_terminate | ( | ) |
Loop until terminate flag was set.
Definition at line 49 of file thread_pool.cpp.
|
delete |
non-copyable: delete assignment operator
size_t size | ( | ) | const |
Return number of threads in pool.
Definition at line 69 of file thread_pool.cpp.
void terminate | ( | ) |
Terminate thread pool gracefully, wait until currently running jobs finish and then exit.
This should be called from within one of the enqueue jobs or from an outside thread.
Definition at line 55 of file thread_pool.cpp.
std::thread & thread | ( | size_t | i | ) |
Return thread handle to thread i.
Definition at line 81 of file thread_pool.cpp.
|
private |
Worker function, one per thread is started.
Definition at line 86 of file thread_pool.cpp.
|
private |
Counter for number of threads busy.
Definition at line 87 of file thread_pool.hpp.
|
private |
Condition variable to signal when a jobs finishes.
Definition at line 84 of file thread_pool.hpp.
|
private |
Condition variable used to notify that a new job has been inserted in the queue.
Definition at line 82 of file thread_pool.hpp.
|
private |
Counter for total number of jobs executed.
Definition at line 91 of file thread_pool.hpp.
|
private |
Counter for number of idle threads waiting for a job.
Definition at line 89 of file thread_pool.hpp.
|
private |
Run once per worker thread.
Definition at line 97 of file thread_pool.hpp.
|
private |
Deque of scheduled jobs.
Definition at line 72 of file thread_pool.hpp.
|
private |
Mutex used to access the queue of scheduled jobs.
Definition at line 75 of file thread_pool.hpp.
|
private |
Flag whether to terminate.
Definition at line 94 of file thread_pool.hpp.
|
private |
threads in pool
Definition at line 78 of file thread_pool.hpp.