ITK  5.0.0
Insight Segmentation and Registration Toolkit
Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | List of all members
itk::ThreadPool Class Reference

#include <itkThreadPool.h>

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

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.

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

void AddThreads (ThreadIdType count)
 
m_WorkQueue emplace_back ([task](){(*task)();})
 
ThreadIdType GetMaximumNumberOfThreads () const
 
virtual const char * GetNameOfClass () const
 
int GetNumberOfCurrentlyIdleThreads () const
 
m_Condition notify_one ()
 
- Public Member Functions inherited from itk::Object
unsigned long AddObserver (const EventObject &event, Command *)
 
unsigned long AddObserver (const EventObject &event, Command *) 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 noexceptoverride
 
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
virtual void Delete ()
 
virtual int GetReferenceCount () const
 
 itkCloneMacro (Self)
 
void Print (std::ostream &os, Indent indent=0) const
 

Static Public Member Functions

static Pointer GetInstance ()
 
static Pointer New ()
 
static bool GetDoNotWaitForThreads ()
 
static void SetDoNotWaitForThreads (bool doNotWaitForThreads)
 
- Static Public Member Functions inherited from itk::Object
static bool GetGlobalWarningDisplay ()
 
static void GlobalWarningDisplayOff ()
 
static void GlobalWarningDisplayOn ()
 
static Pointer New ()
 
static void SetGlobalWarningDisplay (bool flag)
 
- Static Public Member Functions inherited from itk::LightObject
static void BreakOnError ()
 
static Pointer New ()
 

Public Attributes

std::future< return_type > res = task->get_future()
 
return res
 
auto task
 

Protected Member Functions

std::mutex & GetMutex ()
 
 ThreadPool ()
 
 ~ThreadPool () override
 
- Protected Member Functions inherited from itk::Object
 Object ()
 
bool PrintObservers (std::ostream &os, Indent indent) const
 
void PrintSelf (std::ostream &os, Indent indent) const override
 
virtual void SetTimeStamp (const TimeStamp &time)
 
 ~Object () override
 
- 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 ()
 

Private Member Functions

 itkGetGlobalDeclarationMacro (ThreadPoolGlobals, PimplGlobals)
 

Static Private Member Functions

static void ThreadExecute ()
 

Private Attributes

std::condition_variable m_Condition
 
bool m_Stopping { false }
 
std::vector< std::thread > m_Threads
 
std::deque< std::function
< void() > > 
m_WorkQueue
 

Static Private Attributes

static ThreadPoolGlobals * m_PimplGlobals
 

Additional Inherited Members

- Protected Attributes inherited from itk::LightObject
std::atomic< int > m_ReferenceCount
 

Member Typedef Documentation

Definition at line 64 of file itkThreadPool.h.

Definition at line 63 of file itkThreadPool.h.

Standard class type aliases.

Definition at line 61 of file itkThreadPool.h.

Definition at line 62 of file itkThreadPool.h.

Constructor & Destructor Documentation

itk::ThreadPool::ThreadPool ( )
protected
itk::ThreadPool::~ThreadPool ( )
overrideprotected

Member Function Documentation

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

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

m_WorkQueue itk::ThreadPool::emplace_back ( [task] (){(*task)();}  )
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).

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

Returns the global singleton instance of the ThreadPool

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

Definition at line 104 of file itkThreadPool.h.

std::mutex& itk::ThreadPool::GetMutex ( )
protected
virtual const char* itk::ThreadPool::GetNameOfClass ( ) const
virtual

Run-time type information (and related methods).

Reimplemented from itk::Object.

int itk::ThreadPool::GetNumberOfCurrentlyIdleThreads ( ) const

The approximate number of idle threads.

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

Only used to synchronize the global variable across static libraries.

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

Returns the global instance

m_Condition itk::ThreadPool::notify_one ( )
static void itk::ThreadPool::SetDoNotWaitForThreads ( bool  doNotWaitForThreads)
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).

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

The continuously running thread function

Member Data Documentation

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 141 of file itkThreadPool.h.

ThreadPoolGlobals* itk::ThreadPool::m_PimplGlobals
staticprivate

To lock on the internal variables

Definition at line 151 of file itkThreadPool.h.

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

Definition at line 148 of file itkThreadPool.h.

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 145 of file itkThreadPool.h.

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 137 of file itkThreadPool.h.

std::future< return_type > itk::ThreadPool::res = task->get_future()

Definition at line 92 of file itkThreadPool.h.

return itk::ThreadPool::res

Definition at line 98 of file itkThreadPool.h.

auto itk::ThreadPool::task
Initial value:
= std::make_shared< std::packaged_task< return_type() > >(
std::bind( std::forward< Function >( function ), std::forward< Arguments >( arguments )... ) )

Definition at line 89 of file itkThreadPool.h.


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