Alexandria  2.16
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Types | Public Member Functions | Private Attributes | List of all members
Euclid::ThreadPool Class Reference

Basic thread pool implementation. More...

#include <ThreadPool.h>

Collaboration diagram for Euclid::ThreadPool:
[legend]

Public Types

using Task = std::function< void(void)>
 The type of tasks the pool can execute. More...
 

Public Member Functions

 ThreadPool (unsigned int thread_count=std::thread::hardware_concurrency(), unsigned int empty_queue_wait_time=50)
 Constructs a new ThreadPool. More...
 
virtual ~ThreadPool ()
 
void submit (Task task)
 Submit a task to be executed. More...
 
void block ()
 
bool checkForException (bool rethrow=false)
 Checks if any task has thrown an exception and optionally rethrows it. More...
 

Private Attributes

std::mutex m_queue_mutex {}
 
std::vector< std::atomic< bool > > m_worker_run_flags
 
std::vector< std::atomic< bool > > m_worker_sleeping_flags
 
std::vector< std::atomic< bool > > m_worker_done_flags
 
std::deque< Taskm_queue {}
 
unsigned int m_empty_queue_wait_time
 
std::exception_ptr m_exception_ptr
 

Detailed Description

Basic thread pool implementation.

This class provides a basic thread pool implementation, to be used when the boost thread pool is not available (boost versions earlier than 1.56). If your boost version contains the thread pool (boost/thread/thread_pool.hpp) use this one instead.

Using the pool is quite simple. The constructor of the ThreadPool gets as parameter the number of threads that will be spawned (defaults to the number of threads available). The ThreadPool::submit() method can be used to submit tasks to the thread pool queue. Tasks can be anything that can be assigned to a std::function object which does not get any parameters and returns void. The thread pool will assign all tasks to the threads to be executed at the same order as they are submitted. To block until all the tasks in the pool have been executed, one can all the ThreadPool::block() method.

Note that when the ThreadPool object goes out of scope and its destructor is called it will not process any tasks that are not already started, but it will block until all threads finish with the currently executing tasks.

If any of the tasks in the queue throws an exception, all other running tasks will finish their execution, but no new tasks will be started. In this case, the block() method will rethrow the exception. The pool can be checked if it is in an exception state by calling the checkForException() method.

Definition at line 68 of file ThreadPool.h.

Member Typedef Documentation

The type of tasks the pool can execute.

Definition at line 73 of file ThreadPool.h.

Constructor & Destructor Documentation

Euclid::ThreadPool::ThreadPool ( unsigned int  thread_count = std::thread::hardware_concurrency(),
unsigned int  empty_queue_wait_time = 50 
)
explicit

Constructs a new ThreadPool.

Parameters
thread_countThe number of threads in the pool (defaults to the number of available cores)
empty_queue_wait_timeThe time (in milliseconds) the pool threads sleep after they try to get a task from an empty queue before they retry

Definition at line 89 of file ThreadPool.cpp.

References std::vector< T >::at(), m_empty_queue_wait_time, m_exception_ptr, m_queue, m_queue_mutex, m_worker_done_flags, m_worker_run_flags, and m_worker_sleeping_flags.

Here is the call graph for this function:

Euclid::ThreadPool::~ThreadPool ( )
virtual

All tasks not yet started are discarded and it blocks until all already executing tasks are finished

Definition at line 143 of file ThreadPool.cpp.

References m_empty_queue_wait_time, m_worker_done_flags, and m_worker_run_flags.

Member Function Documentation

void Euclid::ThreadPool::block ( )

Blocks the calling thread until all the tasks in the pool queue are finished. Note that submitting tasks until this method returns is not allowed.

Definition at line 125 of file ThreadPool.cpp.

References checkForException(), m_empty_queue_wait_time, m_exception_ptr, m_queue, m_queue_mutex, m_worker_sleeping_flags, and std::this_thread::sleep_for().

Here is the call graph for this function:

bool Euclid::ThreadPool::checkForException ( bool  rethrow = false)

Checks if any task has thrown an exception and optionally rethrows it.

Definition at line 114 of file ThreadPool.cpp.

References m_exception_ptr, and std::rethrow_exception().

Referenced by block().

Here is the call graph for this function:

void Euclid::ThreadPool::submit ( Task  task)

Submit a task to be executed.

Definition at line 153 of file ThreadPool.cpp.

References m_queue, m_queue_mutex, and std::move().

Here is the call graph for this function:

Member Data Documentation

unsigned int Euclid::ThreadPool::m_empty_queue_wait_time
private

Definition at line 106 of file ThreadPool.h.

Referenced by block(), ThreadPool(), and ~ThreadPool().

std::exception_ptr Euclid::ThreadPool::m_exception_ptr
private

Definition at line 107 of file ThreadPool.h.

Referenced by block(), checkForException(), and ThreadPool().

std::deque<Task> Euclid::ThreadPool::m_queue {}
private

Definition at line 105 of file ThreadPool.h.

Referenced by block(), submit(), and ThreadPool().

std::mutex Euclid::ThreadPool::m_queue_mutex {}
private

Definition at line 101 of file ThreadPool.h.

Referenced by block(), submit(), and ThreadPool().

std::vector<std::atomic<bool> > Euclid::ThreadPool::m_worker_done_flags
private

Definition at line 104 of file ThreadPool.h.

Referenced by ThreadPool(), and ~ThreadPool().

std::vector<std::atomic<bool> > Euclid::ThreadPool::m_worker_run_flags
private

Definition at line 102 of file ThreadPool.h.

Referenced by ThreadPool(), and ~ThreadPool().

std::vector<std::atomic<bool> > Euclid::ThreadPool::m_worker_sleeping_flags
private

Definition at line 103 of file ThreadPool.h.

Referenced by block(), and ThreadPool().


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