Alexandria 2.31.0
SDC-CH common library for the Euclid project
Loading...
Searching...
No Matches
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:
Collaboration graph
[legend]

Public Types

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

Public Member Functions

 ThreadPool (unsigned int thread_count=std::thread::hardware_concurrency(), unsigned int empty_queue_wait_time=50)
 Constructs a new ThreadPool.
 
virtual ~ThreadPool ()
 
void submit (Task task)
 Submit a task to be executed.
 
void block (bool throw_on_exception=true)
 
bool checkForException (bool rethrow=false)
 Checks if any task has thrown an exception and optionally rethrows it.
 
size_t queued () const
 Return the number of queued tasks.
 
size_t running () const
 Return the number of running tasks.
 
size_t activeThreads () const
 Return the number of active workers (either running or sleeping)
 

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::vector< std::threadm_workers
 
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

◆ Task

The type of tasks the pool can execute.

Definition at line 72 of file ThreadPool.h.

Constructor & Destructor Documentation

◆ ThreadPool()

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 97 of file ThreadPool.cpp.

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

Here is the call graph for this function:

◆ ~ThreadPool()

Euclid::ThreadPool::~ThreadPool ( )
virtual

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

Definition at line 173 of file ThreadPool.cpp.

References std::vector< T >::begin(), block(), std::vector< T >::end(), std::fill(), m_worker_run_flags, and m_workers.

Here is the call graph for this function:

Member Function Documentation

◆ activeThreads()

size_t Euclid::ThreadPool::activeThreads ( ) const

Return the number of active workers (either running or sleeping)

Definition at line 147 of file ThreadPool.cpp.

References std::accumulate(), std::vector< T >::begin(), std::vector< T >::end(), m_worker_done_flags, and std::vector< T >::size().

Here is the call graph for this function:

◆ block()

void Euclid::ThreadPool::block ( bool  throw_on_exception = true)

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 152 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().

Referenced by ~ThreadPool().

Here is the call graph for this function:

◆ checkForException()

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

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

Definition at line 124 of file ThreadPool.cpp.

References m_exception_ptr, m_queue_mutex, and std::rethrow_exception().

Referenced by block().

Here is the call graph for this function:

◆ queued()

size_t Euclid::ThreadPool::queued ( ) const

Return the number of queued tasks.

Definition at line 136 of file ThreadPool.cpp.

References m_queue, and m_queue_mutex.

◆ running()

size_t Euclid::ThreadPool::running ( ) const

Return the number of running tasks.

Definition at line 141 of file ThreadPool.cpp.

References std::accumulate(), std::vector< T >::begin(), std::vector< T >::end(), m_queue_mutex, m_worker_sleeping_flags, and std::vector< T >::size().

Here is the call graph for this function:

◆ submit()

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

Submit a task to be executed.

Definition at line 184 of file ThreadPool.cpp.

References std::vector< T >::empty(), m_queue, m_queue_mutex, m_worker_run_flags, and std::move().

Here is the call graph for this function:

Member Data Documentation

◆ m_empty_queue_wait_time

unsigned int Euclid::ThreadPool::m_empty_queue_wait_time
private

Definition at line 115 of file ThreadPool.h.

Referenced by block(), and ThreadPool().

◆ m_exception_ptr

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

Definition at line 116 of file ThreadPool.h.

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

◆ m_queue

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

Definition at line 114 of file ThreadPool.h.

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

◆ m_queue_mutex

std::mutex Euclid::ThreadPool::m_queue_mutex
mutableprivate

Definition at line 109 of file ThreadPool.h.

Referenced by block(), checkForException(), queued(), running(), submit(), and ThreadPool().

◆ m_worker_done_flags

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

Definition at line 112 of file ThreadPool.h.

Referenced by activeThreads(), and ThreadPool().

◆ m_worker_run_flags

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

Definition at line 110 of file ThreadPool.h.

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

◆ m_worker_sleeping_flags

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

Definition at line 111 of file ThreadPool.h.

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

◆ m_workers

std::vector<std::thread> Euclid::ThreadPool::m_workers
private

Definition at line 113 of file ThreadPool.h.

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


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