ITK  4.13.0
Insight Segmentation and Registration Toolkit
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected 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 MultiThreader. Initially the thread pool is started with GlobalDefaultNumberOfThreads. The ThreadJob class is used to submit jobs to the thread pool. The ThreadJob's necessary members need to be set and then the ThreadJob can be passed to the ThreadPool by calling its AddWork method. One can then wait for the job by calling the WaitForJob method.

When thread pool is in use by the MultiThreader, invoking SetNumberOfThreads on MultiThreader will only increase the number of jobs submitted to the ThreadPool, it will not increase the number of threads. This trick can be used to increase the number of chunks, which can help load balancing in case the algorithm takes more time for some parts of the image, and there is relatively small overhead for chunking (splitting the image for processing).

If more threads are required, e.g. in case when Barrier is used, AddThreads method should be invoked.

Definition at line 61 of file itkThreadPool.h.

Public Types

typedef SmartPointer< const SelfConstPointer
 
typedef SmartPointer< SelfPointer
 
typedef ThreadPool Self
 
typedef ThreadJob::Semaphore Semaphore
 
typedef Object Superclass
 
- Public Types inherited from itk::Object
typedef SmartPointer< const SelfConstPointer
 
typedef SmartPointer< SelfPointer
 
typedef Object Self
 
typedef LightObject Superclass
 
- Public Types inherited from itk::LightObject
typedef SmartPointer< const SelfConstPointer
 
typedef SmartPointer< SelfPointer
 
typedef LightObject Self
 

Public Member Functions

void AddThreads (ThreadIdType count)
 
void AddWork (const ThreadJob &job)
 
virtual const char * GetNameOfClass () const
 
int GetNumberOfCurrentlyIdleThreads () const
 
void WaitForJob (Semaphore &jobSemaphore)
 
- Public Member Functions inherited from itk::Object
unsigned long AddObserver (const EventObject &event, Command *)
 
unsigned long AddObserver (const EventObject &event, Command *) const
 
virtual 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
 
virtual void Register () const override
 
void RemoveAllObservers ()
 
void RemoveObserver (unsigned long tag)
 
void SetDebug (bool debugFlag) const
 
void SetMetaDataDictionary (const MetaDataDictionary &rhs)
 
virtual void SetReferenceCount (int) override
 
virtual void UnRegister () const noexceptoverride
 
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 ThreadIdType GetGlobalDefaultNumberOfThreads ()
 
static ThreadIdType GetGlobalDefaultNumberOfThreadsByPlatform ()
 
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 flag)
 
- Static Public Member Functions inherited from itk::LightObject
static void BreakOnError ()
 
static Pointer New ()
 

Protected Member Functions

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

Static Protected Member Functions

static bool PlatformClose (ThreadProcessIdType &threadId)
 
static void PlatformCreate (Semaphore &semaphore)
 
static void PlatformDelete (Semaphore &semaphore)
 
static void PlatformSignal (Semaphore &semaphore)
 
static void PlatformWait (Semaphore &semaphore)
 

Static Private Member Functions

static ITK_THREAD_RETURN_TYPE ThreadExecute (void *param)
 

Private Attributes

bool m_ExceptionOccurred
 
std::vector< ThreadProcessIdTypem_Threads
 
Semaphore m_ThreadsSemaphore
 
std::deque< ThreadJobm_WorkQueue
 

Static Private Attributes

static SimpleFastMutexLock m_Mutex
 
static Pointer m_ThreadPoolInstance
 

Additional Inherited Members

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

Member Typedef Documentation

Definition at line 69 of file itkThreadPool.h.

Definition at line 68 of file itkThreadPool.h.

Standard class typedefs.

Definition at line 66 of file itkThreadPool.h.

typedef ThreadJob::Semaphore itk::ThreadPool::Semaphore

Definition at line 71 of file itkThreadPool.h.

Definition at line 67 of file itkThreadPool.h.

Constructor & Destructor Documentation

itk::ThreadPool::ThreadPool ( )
protected
virtual itk::ThreadPool::~ThreadPool ( )
overrideprotectedvirtual

Member Function Documentation

void itk::ThreadPool::AddThread ( )
protected

Called to add a thread to the thread pool. This method add a thread to the thread pool and pushes the thread handle into the m_Threads vector.

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

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

void itk::ThreadPool::AddWork ( const ThreadJob job)

Add this job to the thread pool queue. All data members of the ThreadJob must be filled. The semaphore pointer must point to a valid semaphore structure. AddWork will initialize that semaphore, and the invoker must pass it to WaitForJob in order to wait for the job's completion.

void itk::ThreadPool::DeleteThreads ( )
protected

Platform-specific function to clean up all the threads.

static ThreadIdType itk::ThreadPool::GetGlobalDefaultNumberOfThreads ( )
static

Examines environment variables and falls back to hyper-threaded core count

static ThreadIdType itk::ThreadPool::GetGlobalDefaultNumberOfThreadsByPlatform ( )
static

Platform specific number of threads

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

Returns the global singleton instance of the ThreadPool

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.

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

Returns the global instance

static bool itk::ThreadPool::PlatformClose ( ThreadProcessIdType threadId)
staticprotected
static void itk::ThreadPool::PlatformCreate ( Semaphore semaphore)
staticprotected
static void itk::ThreadPool::PlatformDelete ( Semaphore semaphore)
staticprotected
static void itk::ThreadPool::PlatformSignal ( Semaphore semaphore)
staticprotected
static void itk::ThreadPool::PlatformWait ( Semaphore semaphore)
staticprotected
static ITK_THREAD_RETURN_TYPE itk::ThreadPool::ThreadExecute ( void *  param)
staticprivate

The continuously running thread function

void itk::ThreadPool::WaitForJob ( Semaphore jobSemaphore)

This method blocks until the given job has finished executing.

Member Data Documentation

bool itk::ThreadPool::m_ExceptionOccurred
private

Set if exception occurs

Definition at line 126 of file itkThreadPool.h.

SimpleFastMutexLock itk::ThreadPool::m_Mutex
staticprivate

To lock on the internal variables

Definition at line 148 of file itkThreadPool.h.

Pointer itk::ThreadPool::m_ThreadPoolInstance
staticprivate

Definition at line 150 of file itkThreadPool.h.

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

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

Definition at line 145 of file itkThreadPool.h.

Semaphore itk::ThreadPool::m_ThreadsSemaphore
private

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

Definition at line 140 of file itkThreadPool.h.

std::deque<ThreadJob> itk::ThreadPool::m_WorkQueue
private

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

Definition at line 135 of file itkThreadPool.h.


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