ITK  5.2.0
Insight Toolkit
Public Types | Public Member Functions | Static Public Member Functions | List of all members
itk::ThreadPool Class Reference

#include <itkThreadPool.h>

+ Inheritance diagram for itk::ThreadPool:
+ Collaboration diagram for itk::ThreadPool:

Public Types

using ConstPointer = SmartPointer< const Self >
 
using Pointer = SmartPointer< Self >
 
using Self = ThreadPool
 
using Superclass = Object
 
- Public Types inherited from itk::Object
using ConstPointer = SmartPointer< const Self >
 
using Pointer = SmartPointer< Self >
 
using Self = Object
 
using Superclass = LightObject
 
- Public Types inherited from itk::LightObject
using ConstPointer = SmartPointer< const Self >
 
using Pointer = SmartPointer< Self >
 
using Self = LightObject
 

Public Member Functions

virtual const char * GetNameOfClass () const
 
- Public Member Functions inherited from itk::Object
unsigned long AddObserver (const EventObject &event, Command *)
 
unsigned long AddObserver (const EventObject &event, Command *) const
 
unsigned long AddObserver (const EventObject &event, std::function< void(const EventObject &)> function) const
 
LightObject::Pointer CreateAnother () const override
 
virtual void DebugOff () const
 
virtual void DebugOn () const
 
CommandGetCommand (unsigned long tag)
 
bool GetDebug () const
 
MetaDataDictionaryGetMetaDataDictionary ()
 
const MetaDataDictionaryGetMetaDataDictionary () const
 
virtual ModifiedTimeType GetMTime () const
 
virtual const TimeStampGetTimeStamp () const
 
bool HasObserver (const EventObject &event) const
 
void InvokeEvent (const EventObject &)
 
void InvokeEvent (const EventObject &) const
 
virtual void Modified () const
 
void Register () const override
 
void RemoveAllObservers ()
 
void RemoveObserver (unsigned long tag)
 
void SetDebug (bool debugFlag) const
 
void SetReferenceCount (int) override
 
void UnRegister () const noexcept override
 
void SetMetaDataDictionary (const MetaDataDictionary &rhs)
 
void SetMetaDataDictionary (MetaDataDictionary &&rrhs)
 
virtual void SetObjectName (std::string _arg)
 
virtual const std::string & GetObjectName () const
 
- Public Member Functions inherited from itk::LightObject
Pointer Clone () const
 
virtual void Delete ()
 
virtual int GetReferenceCount () const
 
void Print (std::ostream &os, Indent indent=0) const
 

Static Public Member Functions

static Pointer GetInstance ()
 
static Pointer New ()
 
- Static Public Member Functions inherited from itk::Object
static bool GetGlobalWarningDisplay ()
 
static void GlobalWarningDisplayOff ()
 
static void GlobalWarningDisplayOn ()
 
static Pointer New ()
 
static void SetGlobalWarningDisplay (bool val)
 
- Static Public Member Functions inherited from itk::LightObject
static void BreakOnError ()
 
static Pointer New ()
 
std::deque< std::function< void()> > m_WorkQueue
 
std::condition_variable m_Condition
 
std::vector< std::thread > m_Threads
 
bool m_Stopping { false }
 
static ThreadPoolGlobals * m_PimplGlobals
 
template<class Function , class... Arguments>
auto AddWork (Function &&function, Arguments &&... arguments) -> std::future< typename std::result_of< Function(Arguments...)>::type >
 
void AddThreads (ThreadIdType count)
 
ThreadIdType GetMaximumNumberOfThreads () const
 
int GetNumberOfCurrentlyIdleThreads () const
 
static bool GetDoNotWaitForThreads ()
 
static void SetDoNotWaitForThreads (bool doNotWaitForThreads)
 
std::mutex & GetMutex ()
 
 ThreadPool ()
 
 ~ThreadPool () override
 
 itkGetGlobalDeclarationMacro (ThreadPoolGlobals, PimplGlobals)
 
static void ThreadExecute ()
 

Additional Inherited Members

- Protected Member Functions inherited from itk::Object
 Object ()
 
 ~Object () override
 
void PrintSelf (std::ostream &os, Indent indent) const override
 
bool PrintObservers (std::ostream &os, Indent indent) const
 
virtual void SetTimeStamp (const TimeStamp &timeStamp)
 
- Protected Member Functions inherited from itk::LightObject
virtual LightObject::Pointer InternalClone () const
 
 LightObject ()
 
virtual void PrintHeader (std::ostream &os, Indent indent) const
 
virtual void PrintTrailer (std::ostream &os, Indent indent) const
 
virtual ~LightObject ()
 
- Protected Attributes inherited from itk::LightObject
std::atomic< int > m_ReferenceCount
 

Detailed Description

Thread pool maintains a constant number of threads.

Thread pool is called and initialized from within the PoolMultiThreader. Initially the thread pool is started with GlobalDefaultNumberOfThreads. The jobs are submitted via AddWork method.

This implementation heavily borrows from: https://github.com/progschj/ThreadPool

Definition at line 55 of file itkThreadPool.h.

Member Typedef Documentation

◆ ConstPointer

Definition at line 64 of file itkThreadPool.h.

◆ Pointer

Definition at line 63 of file itkThreadPool.h.

◆ Self

Standard class type aliases.

Definition at line 61 of file itkThreadPool.h.

◆ Superclass

Definition at line 62 of file itkThreadPool.h.

Constructor & Destructor Documentation

◆ ThreadPool()

itk::ThreadPool::ThreadPool ( )
protected

This is a list of jobs submitted to the thread pool. This is the only place where the jobs are submitted. Filled by AddWork, emptied by ThreadExecute.

◆ ~ThreadPool()

itk::ThreadPool::~ThreadPool ( )
overrideprotected

This is a list of jobs submitted to the thread pool. This is the only place where the jobs are submitted. Filled by AddWork, emptied by ThreadExecute.

Member Function Documentation

◆ AddThreads()

void itk::ThreadPool::AddThreads ( ThreadIdType  count)

Can call this method if we want to add extra threads to the pool.

◆ AddWork()

template<class Function , class... Arguments>
auto itk::ThreadPool::AddWork ( Function &&  function,
Arguments &&...  arguments 
) -> std::future<typename std::result_of<Function(Arguments...)>::type>
inline

Add this job to the thread pool queue.

This method returns an std::future, and calling get()
will block until the result is ready. Example usage:
auto result = pool->AddWork([](int param) { return param; }, 7);

std::cout << result.get() << std::endl;

Definition at line 87 of file itkThreadPool.h.

◆ GetDoNotWaitForThreads()

static bool itk::ThreadPool::GetDoNotWaitForThreads ( )
static

Set/Get wait for threads. This function should be used carefully, probably only during static initialization phase to disable waiting for threads when ITK is built as a static library and linked into a shared library (Windows only).

◆ GetInstance()

static Pointer itk::ThreadPool::GetInstance ( )
static

Returns the global singleton instance of the ThreadPool

◆ GetMaximumNumberOfThreads()

ThreadIdType itk::ThreadPool::GetMaximumNumberOfThreads ( ) const
inline

This is a list of jobs submitted to the thread pool. This is the only place where the jobs are submitted. Filled by AddWork, emptied by ThreadExecute.

Definition at line 110 of file itkThreadPool.h.

◆ GetMutex()

std::mutex& itk::ThreadPool::GetMutex ( )
protected

This is a list of jobs submitted to the thread pool. This is the only place where the jobs are submitted. Filled by AddWork, emptied by ThreadExecute.

◆ GetNameOfClass()

virtual const char* itk::ThreadPool::GetNameOfClass ( ) const
virtual

Run-time type information (and related methods).

Reimplemented from itk::Object.

◆ GetNumberOfCurrentlyIdleThreads()

int itk::ThreadPool::GetNumberOfCurrentlyIdleThreads ( ) const

The approximate number of idle threads.

◆ itkGetGlobalDeclarationMacro()

itk::ThreadPool::itkGetGlobalDeclarationMacro ( ThreadPoolGlobals  ,
PimplGlobals   
)
private

Only used to synchronize the global variable across static libraries.

◆ New()

static Pointer itk::ThreadPool::New ( )
static

Returns the global instance

◆ SetDoNotWaitForThreads()

static void itk::ThreadPool::SetDoNotWaitForThreads ( bool  doNotWaitForThreads)
static

This is a list of jobs submitted to the thread pool. This is the only place where the jobs are submitted. Filled by AddWork, emptied by ThreadExecute.

◆ ThreadExecute()

static void itk::ThreadPool::ThreadExecute ( )
staticprivate

The continuously running thread function

Member Data Documentation

◆ m_Condition

std::condition_variable itk::ThreadPool::m_Condition
private

When a thread is idle, it is waiting on m_Condition. AddWork signals it to resume a (random) thread.

Definition at line 149 of file itkThreadPool.h.

◆ m_PimplGlobals

ThreadPoolGlobals* itk::ThreadPool::m_PimplGlobals
staticprivate

To lock on the internal variables

Definition at line 159 of file itkThreadPool.h.

◆ m_Stopping

bool itk::ThreadPool::m_Stopping { false }
private

This is a list of jobs submitted to the thread pool. This is the only place where the jobs are submitted. Filled by AddWork, emptied by ThreadExecute.

Definition at line 156 of file itkThreadPool.h.

◆ m_Threads

std::vector<std::thread> itk::ThreadPool::m_Threads
private

Vector to hold all thread handles. Thread handles are used to delete (join) the threads.

Definition at line 153 of file itkThreadPool.h.

◆ m_WorkQueue

std::deque<std::function<void()> > itk::ThreadPool::m_WorkQueue
private

This is a list of jobs submitted to the thread pool. This is the only place where the jobs are submitted. Filled by AddWork, emptied by ThreadExecute.

Definition at line 145 of file itkThreadPool.h.


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