Mantid
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Protected Attributes | Private Member Functions | List of all members
Mantid::Kernel::ThreadPool Class Referencefinal

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< ProgressBasem_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< ThreadSchedulerm_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

ThreadPooloperator= (const ThreadPool &other)
 
 ThreadPool (const ThreadPool &)
 

Detailed Description

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.

Author
Janik Zikovsky, SNS
Date
Feb 7, 2011

Definition at line 36 of file ThreadPool.h.

Constructor & Destructor Documentation

◆ ThreadPool() [1/2]

Mantid::Kernel::ThreadPool::ThreadPool ( ThreadScheduler scheduler = new ThreadSchedulerFIFO(),
size_t  numThreads = 0,
ProgressBase prog = nullptr 
)

Constructor.

Parameters
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.

◆ ~ThreadPool()

Mantid::Kernel::ThreadPool::~ThreadPool ( )
default

Destructor.

Deletes the ThreadScheduler.

◆ ThreadPool() [2/2]

Mantid::Kernel::ThreadPool::ThreadPool ( const ThreadPool )
private

Member Function Documentation

◆ getNumPhysicalCores()

size_t Mantid::Kernel::ThreadPool::getNumPhysicalCores ( )
static

Return the number of physical cores available on the system.

NOTE: Uses OPENMP or Poco::Environment::processorCount() to find the number.

Returns
how many cores are present.

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

◆ joinAll()

void Mantid::Kernel::ThreadPool::joinAll ( )

Wait for all threads that have started to finish.

If the threads had not started, start them first.

Exceptions
std::runtime_errorrethrown 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().

◆ operator=()

ThreadPool & Mantid::Kernel::ThreadPool::operator= ( const ThreadPool other)
private

◆ schedule()

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.

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

◆ start()

void Mantid::Kernel::ThreadPool::start ( double  waitSec = 0.0)

Start the threads and begin looking for tasks.

Parameters
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!
Exceptions
runtime_errorif 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().

Member Data Documentation

◆ m_numThreads

size_t Mantid::Kernel::ThreadPool::m_numThreads
protected

Number of cores used.

Definition at line 53 of file ThreadPool.h.

Referenced by start(), and ThreadPool().

◆ m_prog

std::unique_ptr<ProgressBase> Mantid::Kernel::ThreadPool::m_prog
protected

Progress reporter.

Definition at line 68 of file ThreadPool.h.

Referenced by start().

◆ m_runnables

std::vector<std::unique_ptr<ThreadPoolRunnable> > Mantid::Kernel::ThreadPool::m_runnables
protected

Vector of the POCO-required classes to actually run in a thread.

Definition at line 62 of file ThreadPool.h.

Referenced by joinAll(), and start().

◆ m_scheduler

std::unique_ptr<ThreadScheduler> Mantid::Kernel::ThreadPool::m_scheduler
protected

The ThreadScheduler instance taking care of task scheduling.

Definition at line 56 of file ThreadPool.h.

Referenced by joinAll(), schedule(), start(), and ThreadPool().

◆ m_started

bool Mantid::Kernel::ThreadPool::m_started
protected

Have the threads started?

Definition at line 65 of file ThreadPool.h.

Referenced by joinAll(), schedule(), and start().

◆ m_threads

std::vector<std::unique_ptr<Poco::Thread> > Mantid::Kernel::ThreadPool::m_threads
protected

Vector with all the threads that are started.

Definition at line 59 of file ThreadPool.h.

Referenced by joinAll(), and start().


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