ITK
6.0.0
Insight Toolkit
|
#include <itkThreadPool.h>
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) |
ThreadIdType | GetMaximumNumberOfThreads () const |
const char * | GetNameOfClass () const override |
int | GetNumberOfCurrentlyIdleThreads () const |
template<class Function , class... Arguments> | |
auto | AddWork (Function &&function, Arguments &&... arguments) -> std::future< std::invoke_result_t< Function, Arguments... >> |
Public Member Functions inherited from itk::Object | |
unsigned long | AddObserver (const EventObject &event, Command *cmd) 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 |
Command * | GetCommand (unsigned long tag) |
bool | GetDebug () const |
MetaDataDictionary & | GetMetaDataDictionary () |
const MetaDataDictionary & | GetMetaDataDictionary () const |
virtual ModifiedTimeType | GetMTime () const |
virtual const TimeStamp & | GetTimeStamp () 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) const |
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 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 val) |
Static Public Member Functions inherited from itk::LightObject | |
static void | BreakOnError () |
static Pointer | New () |
Protected Member Functions | |
void | CleanUp () |
std::mutex & | GetMutex () const |
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 &timeStamp) |
~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 void | PrepareForFork () |
static void | ResumeFromFork () |
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 {} |
using itk::ThreadPool::ConstPointer = SmartPointer<const Self> |
Definition at line 64 of file itkThreadPool.h.
using itk::ThreadPool::Pointer = SmartPointer<Self> |
Definition at line 63 of file itkThreadPool.h.
using itk::ThreadPool::Self = ThreadPool |
Standard class type aliases.
Definition at line 61 of file itkThreadPool.h.
using itk::ThreadPool::Superclass = Object |
Definition at line 62 of file itkThreadPool.h.
|
protected |
|
inlineoverrideprotected |
Definition at line 141 of file itkThreadPool.h.
void itk::ThreadPool::AddThreads | ( | ThreadIdType | count | ) |
Can call this method if we want to add extra threads to the pool.
|
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:
std::cout << result.get() << std::endl;
Definition at line 87 of file itkThreadPool.h.
|
protected |
Stop the pool and release threads. To be called by the destructor and atfork.
|
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 |
Returns the global singleton instance of the ThreadPool
|
inline |
Definition at line 109 of file itkThreadPool.h.
|
protected |
We need access to the mutex in AddWork, and the variable is only visible in the .cxx file, so this method returns it.
|
overridevirtual |
Reimplemented from itk::Object.
int itk::ThreadPool::GetNumberOfCurrentlyIdleThreads | ( | ) | const |
The approximate number of idle threads.
|
private |
Only used to synchronize the global variable across static libraries.
|
static |
Returns the global instance
|
staticprotected |
|
staticprotected |
|
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).
|
staticprivate |
The continuously running thread function
|
private |
When a thread is idle, it is waiting on m_Condition. AddWork signals it to resume a (random) thread.
Definition at line 159 of file itkThreadPool.h.
|
staticprivate |
To lock on the internal variables
Definition at line 169 of file itkThreadPool.h.
|
private |
Definition at line 166 of file itkThreadPool.h.
|
private |
Vector to hold all thread handles. Thread handles are used to delete (join) the threads.
Definition at line 163 of file itkThreadPool.h.
|
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 155 of file itkThreadPool.h.