ITK  4.9.0
Insight Segmentation and Registration Toolkit
Classes | Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Types | 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 manages the threads for itk.

Thread pool is called and initialized from within the MultiThreader. Initially the thread pool is started with zero threads. Threads are added as job(s) are submitted to the thread pool and if it cannot be executed right away. For example : If the thread pool has three threads and all are busy. If a new job is submitted to the thread pool, the thread pool checks to see if any threads are free. If not, it adds a new thread and executed the job right away. 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 ThreaadPool by calling its AssignWork Method which returns the thread id on which the job is being executed. One can then wait for the job using the thread id and calling the WaitForJob method on the thread pool.

Definition at line 72 of file itkThreadPool.h.

Classes

class  ThreadProcessIdentifiers
 
class  ThreadSemaphorePair
 

Public Types

typedef SmartPointer< const SelfConstPointer
 
typedef SmartPointer< SelfPointer
 
typedef ThreadPool Self
 
typedef Object Superclass
 
typedef unsigned int ThreadCountType
 
typedef ThreadJob::JobIdType ThreadJobIdType
 
typedef int ThreadTimeType
 
- 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

ThreadProcessIdType AssignWork (ThreadJob worker)
 
virtual const char * GetNameOfClass () const
 
void InitializeThreads (ThreadCountType maxThreads)
 
bool WaitForJobOnThreadHandle (ThreadProcessIdType handle)
 
- 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 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

 ThreadPool ()
 
virtual ~ThreadPool ()
 
- 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 ()
 
- 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 Types

enum  {
  JOB_THREADHANDLE_JUST_ADDED =-3,
  JOB_THREADHANDLE_IS_FREE =-2,
  JOB_THREADHANDLE_IS_DONE =-1
}
 
typedef std::pair
< ThreadJobIdType, ThreadJob
ThreadJobContainerPairType
 
typedef std::map
< ThreadJobIdType, ThreadJob
ThreadJobContainerType
 
typedef std::set< ThreadJobIdTypeThreadJobIdsContainerType
 
typedef std::set
< ThreadProcessIdType
ThreadProcessIdContainerType
 

Private Member Functions

ThreadPooloperator= (ThreadPool const &) ITK_DELETE_FUNCTION
 
 ThreadPool (ThreadPool const &) ITK_DELETE_FUNCTION
 

Private Attributes

bool m_ExceptionOccurred
 
unsigned int m_IdCounter
 
bool m_ScheduleForDestruction
 
ThreadCountType m_ThreadCount
 
ThreadProcessIdContainerType m_ThreadHandles
 
ThreadJobContainerType m_WorkerQueue
 

Static Private Attributes

static SimpleFastMutexLock m_NumberOfPendingJobsToBeRunMutex
 
static SimpleFastMutexLock m_WorkerQueueMutex
 
typedef std::vector
< ThreadProcessIdentifiers
ThreadProcessIdentifiersVecType
 
typedef std::vector
< ThreadSemaphorePair * > 
ThreadSemHandlePairingQueueType
 
ThreadProcessIdentifiersVecType m_ThreadProcessIdentifiersVector
 
ThreadSemHandlePairingQueueType m_ThreadSemHandlePairingQueue
 
ThreadSemHandlePairingQueueType m_ThreadSemHandlePairingForWaitQueue
 
static SimpleFastMutexLock m_ThreadProcessIdentifiersVectorMutex
 
static Pointer m_ThreadPoolInstance
 
static SimpleFastMutexLock m_ThreadPoolInstanceMutex
 
const ThreadJobFetchWork (ThreadProcessIdType t)
 
void RemoveActiveId (ThreadJobIdType id)
 
void AddThread ()
 
ThreadProcessIdentifiersFindThreadToRun ()
 
ThreadSemaphorePairGetSemaphore (ThreadSemHandlePairingQueueType &q, ThreadProcessIdType threadHandle)
 
ThreadSemaphorePairGetSemaphoreForThread (ThreadProcessIdType threadHandle)
 
ThreadSemaphorePairGetSemaphoreForThreadWait (ThreadProcessIdType threadHandle)
 
void DeallocateThreadSemSet (ThreadSemHandlePairingQueueType &q)
 
ThreadProcessIdType GetThreadHandleForThreadId (ThreadIdType id)
 
static void * ThreadExecute (void *param)
 
static bool CompareThreadHandles (ThreadProcessIdType t1, ThreadProcessIdType t2)
 

Additional Inherited Members

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

Member Typedef Documentation

Definition at line 80 of file itkThreadPool.h.

Definition at line 79 of file itkThreadPool.h.

Standard class typedefs.

Definition at line 77 of file itkThreadPool.h.

Definition at line 78 of file itkThreadPool.h.

typedef unsigned int itk::ThreadPool::ThreadCountType

Definition at line 84 of file itkThreadPool.h.

Definition at line 157 of file itkThreadPool.h.

Definition at line 155 of file itkThreadPool.h.

Definition at line 159 of file itkThreadPool.h.

Definition at line 85 of file itkThreadPool.h.

Definition at line 161 of file itkThreadPool.h.

To lock on the vectors

Definition at line 226 of file itkThreadPool.h.

To lock on the vectors

Definition at line 230 of file itkThreadPool.h.

local class typedefs.

Definition at line 83 of file itkThreadPool.h.

Member Enumeration Documentation

anonymous enum
private

Vector of pairs that hold job ids and their corresponding thread handles

Enumerator
JOB_THREADHANDLE_JUST_ADDED 
JOB_THREADHANDLE_IS_FREE 
JOB_THREADHANDLE_IS_DONE 

Definition at line 178 of file itkThreadPool.h.

Constructor & Destructor Documentation

itk::ThreadPool::ThreadPool ( )
protected
virtual itk::ThreadPool::~ThreadPool ( )
protectedvirtual
itk::ThreadPool::ThreadPool ( ThreadPool const &  )
private

Member Function Documentation

void itk::ThreadPool::AddThread ( )
private

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_ThreadHandles set

ThreadProcessIdType itk::ThreadPool::AssignWork ( ThreadJob  worker)

This method is called to assign a job to the thread pool

static bool itk::ThreadPool::CompareThreadHandles ( ThreadProcessIdType  t1,
ThreadProcessIdType  t2 
)
staticprivate

Method to compare thread handles - true for same false for different

void itk::ThreadPool::DeallocateThreadSemSet ( ThreadSemHandlePairingQueueType q)
private

To lock on the vectors

const ThreadJob& itk::ThreadPool::FetchWork ( ThreadProcessIdType  t)
private

This function is called by the threads to get jobs to be executed. This method is the "first" call from the threads in the thread pool. Now this method blocks the thread until a job is available for the thread to execute. Once a job is available(known from m_ThreadProcessIdentifiersVector), it gets it from the worker queue and returns it

ThreadProcessIdentifiers* itk::ThreadPool::FindThreadToRun ( )
private

To check if the thread pool has to add a thread. This method checks if any threads in the thread pool are free. If so, it returns false else returns true

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

Returns the global singleton instance of the ThreadPool

This method is a Singleton and does not have a New method.

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

Run-time type information (and related methods).

Reimplemented from itk::Object.

ThreadSemaphorePair* itk::ThreadPool::GetSemaphore ( ThreadSemHandlePairingQueueType q,
ThreadProcessIdType  threadHandle 
)
private

To lock on the vectors

ThreadSemaphorePair* itk::ThreadPool::GetSemaphoreForThread ( ThreadProcessIdType  threadHandle)
private

To lock on the vectors

ThreadSemaphorePair* itk::ThreadPool::GetSemaphoreForThreadWait ( ThreadProcessIdType  threadHandle)
private

To lock on the vectors

ThreadProcessIdType itk::ThreadPool::GetThreadHandleForThreadId ( ThreadIdType  id)
private

Used under windows - gets the thread handle associated with thread id

void itk::ThreadPool::InitializeThreads ( ThreadCountType  maxThreads)

Can call this method if we want to pre-start maxThreads in the thread pool

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

Returns the global instance of the ThreadPool

ThreadPool& itk::ThreadPool::operator= ( ThreadPool const &  )
private
void itk::ThreadPool::RemoveActiveId ( ThreadJobIdType  id)
private

Used to remove an active job id from the queue because it is done. This method is called by the threads once they are done with the job. This method removes the job id from m_ActiveJobIds, marks the thread as free by setting appropriate values in the m_ThreadProcessIdentifiersVector vector and removes the job from m_WorkerQueue

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

thread function

bool itk::ThreadPool::WaitForJobOnThreadHandle ( ThreadProcessIdType  handle)

This method blocks until the given (job) id has finished executing

Member Data Documentation

bool itk::ThreadPool::m_ExceptionOccurred
private

set if exception occurs

Definition at line 153 of file itkThreadPool.h.

unsigned int itk::ThreadPool::m_IdCounter
private

counter to assign job ids

Definition at line 150 of file itkThreadPool.h.

SimpleFastMutexLock itk::ThreadPool::m_NumberOfPendingJobsToBeRunMutex
staticprivate

To lock on m_NumberOfPendingJobsToBeRun

Definition at line 147 of file itkThreadPool.h.

bool itk::ThreadPool::m_ScheduleForDestruction
private

Set when the thread pool is to be stopped

Definition at line 141 of file itkThreadPool.h.

ThreadCountType itk::ThreadPool::m_ThreadCount
private

Maintains count of threads

Definition at line 144 of file itkThreadPool.h.

ThreadProcessIdContainerType itk::ThreadPool::m_ThreadHandles
private

Vector to hold all active thread handles

Definition at line 174 of file itkThreadPool.h.

Pointer itk::ThreadPool::m_ThreadPoolInstance
staticprivate

To lock on the vectors

Definition at line 262 of file itkThreadPool.h.

SimpleFastMutexLock itk::ThreadPool::m_ThreadPoolInstanceMutex
staticprivate

To lock on m_ThreadPoolInstance

Definition at line 264 of file itkThreadPool.h.

ThreadProcessIdentifiersVecType itk::ThreadPool::m_ThreadProcessIdentifiersVector
private

To lock on the vectors

Definition at line 227 of file itkThreadPool.h.

SimpleFastMutexLock itk::ThreadPool::m_ThreadProcessIdentifiersVectorMutex
staticprivate

To lock on the vectors

Definition at line 236 of file itkThreadPool.h.

ThreadSemHandlePairingQueueType itk::ThreadPool::m_ThreadSemHandlePairingForWaitQueue
private

To lock on the vectors

Definition at line 233 of file itkThreadPool.h.

ThreadSemHandlePairingQueueType itk::ThreadPool::m_ThreadSemHandlePairingQueue
private

To lock on the vectors

Definition at line 232 of file itkThreadPool.h.

ThreadJobContainerType itk::ThreadPool::m_WorkerQueue
private

this is a list of jobs(ThreadJob) submitted to the thread pool this is the only place where the jobs are submitted. We need a worker queue because the thread pool assigns work to a thread which is free. So when a job is submitted, it has to be stored somewhere

Definition at line 168 of file itkThreadPool.h.

SimpleFastMutexLock itk::ThreadPool::m_WorkerQueueMutex
staticprivate

To lock on m_WorkerQueue

Definition at line 171 of file itkThreadPool.h.


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