Alexandria 2.31.0
SDC-CH common library for the Euclid project
|
Basic thread pool implementation. More...
#include <ThreadPool.h>
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::thread > | m_workers |
std::deque< Task > | m_queue |
unsigned int | m_empty_queue_wait_time |
std::exception_ptr | m_exception_ptr |
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.
using Euclid::ThreadPool::Task = std::function<void(void)> |
The type of tasks the pool can execute.
Definition at line 72 of file ThreadPool.h.
|
explicit |
Constructs a new ThreadPool.
thread_count | The number of threads in the pool (defaults to the number of available cores) |
empty_queue_wait_time | The 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.
|
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.
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().
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().
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().
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.
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().
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().
|
private |
Definition at line 115 of file ThreadPool.h.
Referenced by block(), and ThreadPool().
|
private |
Definition at line 116 of file ThreadPool.h.
Referenced by block(), checkForException(), and ThreadPool().
|
private |
Definition at line 114 of file ThreadPool.h.
Referenced by block(), queued(), submit(), and ThreadPool().
|
mutableprivate |
Definition at line 109 of file ThreadPool.h.
Referenced by block(), checkForException(), queued(), running(), submit(), and ThreadPool().
|
private |
Definition at line 112 of file ThreadPool.h.
Referenced by activeThreads(), and ThreadPool().
|
private |
Definition at line 110 of file ThreadPool.h.
Referenced by submit(), ThreadPool(), and ~ThreadPool().
|
private |
Definition at line 111 of file ThreadPool.h.
Referenced by block(), running(), and ThreadPool().
|
private |
Definition at line 113 of file ThreadPool.h.
Referenced by ThreadPool(), and ~ThreadPool().