tlx
ThreadPool Class Reference

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...
 
ThreadPooloperator= (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< Jobjobs_
 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...
 

Detailed Description

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

  1. all jobs are done AND all threads are idle, when called with loop_until_empty(), or
  2. until Terminate() is called when run with loop_until_terminate().

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.

ThreadPool pool(4); // pool with 4 threads
int value = 0;
pool.enqueue([&value]() {
// increment value in another thread.
++value;
});
pool.loop_until_empty();

Definition at line 64 of file thread_pool.hpp.

Member Typedef Documentation

using InitThread = Delegate<void (size_t)>

Definition at line 68 of file thread_pool.hpp.

using Job = Delegate<void ()>

Definition at line 67 of file thread_pool.hpp.

Constructor & Destructor Documentation

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.

ThreadPool ( const ThreadPool )
delete

non-copyable: delete copy-constructor

~ThreadPool ( )

Stop processing jobs, terminate threads.

Definition at line 25 of file thread_pool.cpp.

Member Function Documentation

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.

ThreadPool& operator= ( const ThreadPool )
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.

void worker ( size_t  p)
private

Worker function, one per thread is started.

Definition at line 86 of file thread_pool.cpp.

Member Data Documentation

std::atomic<size_t> busy_
private

Counter for number of threads busy.

Definition at line 87 of file thread_pool.hpp.

std::condition_variable cv_finished_
private

Condition variable to signal when a jobs finishes.

Definition at line 84 of file thread_pool.hpp.

std::condition_variable cv_jobs_
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.

std::atomic<size_t> done_
private

Counter for total number of jobs executed.

Definition at line 91 of file thread_pool.hpp.

std::atomic<size_t> idle_
private

Counter for number of idle threads waiting for a job.

Definition at line 89 of file thread_pool.hpp.

InitThread init_thread_
private

Run once per worker thread.

Definition at line 97 of file thread_pool.hpp.

std::deque<Job> jobs_
private

Deque of scheduled jobs.

Definition at line 72 of file thread_pool.hpp.

std::mutex mutex_
private

Mutex used to access the queue of scheduled jobs.

Definition at line 75 of file thread_pool.hpp.

std::atomic<bool> terminate_
private

Flag whether to terminate.

Definition at line 94 of file thread_pool.hpp.

simple_vector<std::thread> threads_
private

threads in pool

Definition at line 78 of file thread_pool.hpp.


The documentation for this class was generated from the following files: