18 #ifndef itkThreadPool_h
19 #define itkThreadPool_h
21 #include "itkConfigure.h"
27 #include <condition_variable>
53 struct ThreadPoolGlobals;
81 template<
class Function,
class... Arguments >
83 AddWork( Function&&
function, Arguments&&... arguments )
84 -> std::future<
typename std::result_of< Function( Arguments... ) >::type >
86 using return_type =
typename std::result_of< Function( Arguments... ) >::type;
89 auto task = std::make_shared< std::packaged_task< return_type() > >(
90 std::bind( std::forward< Function >(
function ), std::forward< Arguments >( arguments )... ) );
92 std::future< return_type > res = task->get_future();
94 std::unique_lock< std::mutex > lock( this->GetMutex() );
95 m_WorkQueue.emplace_back( [task]() { ( *task )(); } );
97 m_Condition.notify_one();
110 int GetNumberOfCurrentlyIdleThreads()
const;
116 static bool GetDoNotWaitForThreads();
117 static void SetDoNotWaitForThreads(
bool doNotWaitForThreads);
124 std::mutex& GetMutex();
148 bool m_Stopping{
false };
154 static void ThreadExecute();
Light weight base class for most itk classes.
std::condition_variable m_Condition
std::deque< std::function< void() > > m_WorkQueue
#define itkGetGlobalDeclarationMacro(Type, VarName)
std::vector< std::thread > m_Threads
ThreadIdType GetMaximumNumberOfThreads() const
unsigned int ThreadIdType
Thread pool maintains a constant number of threads.
Base class for most ITK classes.
static ThreadPoolGlobals * m_PimplGlobals