tlx
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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()>
 

Public Member Functions

 ThreadPool (size_t num_threads=std::thread::hardware_concurrency())
 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...
 
std::thread & thread (size_t i)
 Return thread handle to thread i. More...
 

Private Member Functions

void worker ()
 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...
 
SimpleVector< 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 > done_
 Counter for total number of jobs executed. More...
 
std::atomic< bool > terminate_
 Flag whether to terminate. 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 Job = Delegate<void()>

Definition at line 67 of file thread_pool.hpp.

Constructor & Destructor Documentation

ThreadPool ( size_t  num_threads = std::thread::hardware_concurrency())
explicit

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 24 of file thread_pool.cpp.

Member Function Documentation

size_t done ( ) const
inline

Return number of jobs currently completed.

Definition at line 126 of file thread_pool.hpp.

void enqueue ( Job &&  job)
inline

enqueue a Job, the caller must pass in all context using captures.

Definition at line 107 of file thread_pool.hpp.

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 36 of file thread_pool.cpp.

void loop_until_terminate ( )

Loop until terminate flag was set.

Definition at line 42 of file thread_pool.cpp.

ThreadPool& operator= ( const ThreadPool )
delete

non-copyable: delete assignment operator

size_t size ( ) const
inline

Return number of threads in pool.

Definition at line 129 of file thread_pool.hpp.

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 48 of file thread_pool.cpp.

std::thread& thread ( size_t  i)
inline

Return thread handle to thread i.

Definition at line 132 of file thread_pool.hpp.

void worker ( )
private

Worker function, one per thread is started.

Definition at line 58 of file thread_pool.cpp.

Member Data Documentation

std::atomic<size_t> busy_
private

Counter for number of threads busy.

Definition at line 86 of file thread_pool.hpp.

std::condition_variable cv_finished_
private

Condition variable to signal when a jobs finishes.

Definition at line 83 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 81 of file thread_pool.hpp.

std::atomic<size_t> done_
private

Counter for total number of jobs executed.

Definition at line 88 of file thread_pool.hpp.

std::deque<Job> jobs_
private

Deque of scheduled jobs.

Definition at line 71 of file thread_pool.hpp.

std::mutex mutex_
private

Mutex used to access the queue of scheduled jobs.

Definition at line 74 of file thread_pool.hpp.

std::atomic<bool> terminate_
private

Flag whether to terminate.

Definition at line 91 of file thread_pool.hpp.

SimpleVector<std::thread> threads_
private

threads in pool

Definition at line 77 of file thread_pool.hpp.


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