Alexandria
2.14.1
Please provide a description of the 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. 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< 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 73 of file ThreadPool.h.
Euclid::ThreadPool::ThreadPool | ( | unsigned int | thread_count = std::thread::hardware_concurrency() , |
unsigned int | empty_queue_wait_time = 50 |
||
) |
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 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.
|
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.
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().
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().
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().
|
private |
Definition at line 106 of file ThreadPool.h.
Referenced by block(), ThreadPool(), and ~ThreadPool().
|
private |
Definition at line 107 of file ThreadPool.h.
Referenced by block(), checkForException(), and ThreadPool().
|
private |
Definition at line 105 of file ThreadPool.h.
Referenced by block(), submit(), and ThreadPool().
|
private |
Definition at line 101 of file ThreadPool.h.
Referenced by block(), submit(), and ThreadPool().
|
private |
Definition at line 104 of file ThreadPool.h.
Referenced by ThreadPool(), and ~ThreadPool().
|
private |
Definition at line 102 of file ThreadPool.h.
Referenced by ThreadPool(), and ~ThreadPool().
|
private |
Definition at line 103 of file ThreadPool.h.
Referenced by block(), and ThreadPool().