Mantid
|
A Thread Pool implementation that keeps a certain number of threads running (normally, equal to the number of hardware cores available) and schedules tasks to them in the most efficient way possible. More...
#include <ThreadPool.h>
Public Member Functions | |
void | joinAll () |
Wait for all threads that have started to finish. More... | |
void | schedule (const std::shared_ptr< Task > &task, bool start=false) |
Schedule a task for later execution. More... | |
void | start (double waitSec=0.0) |
Start the threads and begin looking for tasks. More... | |
ThreadPool (ThreadScheduler *scheduler=new ThreadSchedulerFIFO(), size_t numThreads=0, ProgressBase *prog=nullptr) | |
Constructor. More... | |
~ThreadPool () | |
Destructor. More... | |
Static Public Member Functions | |
static size_t | getNumPhysicalCores () |
Return the number of physical cores available on the system. More... | |
Protected Attributes | |
size_t | m_numThreads |
Number of cores used. More... | |
std::unique_ptr< ProgressBase > | m_prog |
Progress reporter. More... | |
std::vector< std::unique_ptr< ThreadPoolRunnable > > | m_runnables |
Vector of the POCO-required classes to actually run in a thread. More... | |
std::unique_ptr< ThreadScheduler > | m_scheduler |
The ThreadScheduler instance taking care of task scheduling. More... | |
bool | m_started |
Have the threads started? More... | |
std::vector< std::unique_ptr< Poco::Thread > > | m_threads |
Vector with all the threads that are started. More... | |
Private Member Functions | |
ThreadPool & | operator= (const ThreadPool &other) |
ThreadPool (const ThreadPool &) | |
A Thread Pool implementation that keeps a certain number of threads running (normally, equal to the number of hardware cores available) and schedules tasks to them in the most efficient way possible.
This implementation will be slanted towards performing many more Task's than there are available cores, so threads will be reused.
Definition at line 36 of file ThreadPool.h.
Mantid::Kernel::ThreadPool::ThreadPool | ( | ThreadScheduler * | scheduler = new ThreadSchedulerFIFO() , |
size_t | numThreads = 0 , |
||
ProgressBase * | prog = nullptr |
||
) |
Constructor.
scheduler | :: an instance of a ThreadScheduler to schedule tasks. NOTE: The ThreadPool destructor will delete this ThreadScheduler. |
numThreads | :: number of cores to use; default = 0, meaning auto-detect all available physical cores. |
prog | :: optional pointer to a Progress reporter object. If passed, then automatic progress reporting will be handled by the thread pool. NOTE: The ThreadPool destructor will delete this. |
Definition at line 43 of file ThreadPool.cpp.
References getNumPhysicalCores(), m_numThreads, and m_scheduler.
|
default |
Destructor.
Deletes the ThreadScheduler.
|
private |
|
static |
Return the number of physical cores available on the system.
NOTE: Uses OPENMP or Poco::Environment::processorCount() to find the number.
Definition at line 67 of file ThreadPool.cpp.
References Mantid::Kernel::SingletonHolder< T >::Instance(), and PARALLEL_GET_MAX_THREADS.
Referenced by Mantid::DataHandling::DefaultEventLoader::DefaultEventLoader(), and ThreadPool().
void Mantid::Kernel::ThreadPool::joinAll | ( | ) |
Wait for all threads that have started to finish.
If the threads had not started, start them first.
std::runtime_error | rethrown if one of the tasks threw something. E.g. CancelException in the case of aborting an algorithm. Any exception gets downgraded to runtime_error. |
Definition at line 150 of file ThreadPool.cpp.
References m_runnables, m_scheduler, m_started, m_threads, and start().
Referenced by Mantid::MDAlgorithms::FitMD::createEventOutputWorkspace(), and Mantid::DataHandling::DefaultEventLoader::load().
|
private |
void Mantid::Kernel::ThreadPool::schedule | ( | const std::shared_ptr< Task > & | task, |
bool | start = false |
||
) |
Schedule a task for later execution.
If the threadpool is running, it will be picked up by the next available thread.
task | :: pointer to a Task object to run. |
start | :: start the thread at the same time; default false |
Definition at line 123 of file ThreadPool.cpp.
References m_scheduler, m_started, and start().
Referenced by Mantid::DataHandling::DefaultEventLoader::load().
void Mantid::Kernel::ThreadPool::start | ( | double | waitSec = 0.0 | ) |
Start the threads and begin looking for tasks.
waitSec | :: how many seconds will each thread be allowed to wait (with no tasks scheduled to it) before exiting. Default 0.0 (exit right away). This allows you to start a ThreadPool before you start adding tasks. You still need to call joinAll() after you've finished! |
runtime_error | if called when it has already started. |
Definition at line 94 of file ThreadPool.cpp.
References m_numThreads, m_prog, m_runnables, m_scheduler, m_started, and m_threads.
Referenced by joinAll(), and schedule().
|
protected |
Number of cores used.
Definition at line 53 of file ThreadPool.h.
Referenced by start(), and ThreadPool().
|
protected |
|
protected |
Vector of the POCO-required classes to actually run in a thread.
Definition at line 62 of file ThreadPool.h.
|
protected |
The ThreadScheduler instance taking care of task scheduling.
Definition at line 56 of file ThreadPool.h.
Referenced by joinAll(), schedule(), start(), and ThreadPool().
|
protected |
Have the threads started?
Definition at line 65 of file ThreadPool.h.
Referenced by joinAll(), schedule(), and start().
|
protected |
Vector with all the threads that are started.
Definition at line 59 of file ThreadPool.h.