18#ifndef itkThreadPool_h
19#define itkThreadPool_h
21#include "itkConfigure.h"
27#include <condition_variable>
53struct ThreadPoolGlobals;
85 template <
class Function,
class... Arguments>
87 AddWork(Function && function, Arguments &&... arguments) -> std::future<std::invoke_result_t<Function, Arguments...>>
89 using return_type = std::invoke_result_t<Function, Arguments...>;
92 auto task = std::make_shared<std::packaged_task<return_type()>>(
93 [function, arguments...]() -> return_type {
return function(arguments...); });
95 std::future<return_type> res = task->get_future();
97 const std::lock_guard<std::mutex> lockGuard(this->GetMutex());
98 m_WorkQueue.emplace_back([task]() { (*task)(); });
100 m_Condition.notify_one();
111 const std::lock_guard<std::mutex> lockGuard(this->GetMutex());
166 bool m_Stopping{
false };
Light weight base class for most itk classes.
Base class for most ITK classes.
Thread pool maintains a constant number of threads.
itkGetGlobalDeclarationMacro(ThreadPoolGlobals, PimplGlobals)
static void ThreadExecute()
static Pointer GetInstance()
static bool GetDoNotWaitForThreads()
ThreadIdType GetMaximumNumberOfThreads() const
static void SetDoNotWaitForThreads(bool doNotWaitForThreads)
static void ResumeFromFork()
static void PrepareForFork()
std::mutex & GetMutex() const
std::vector< std::thread > m_Threads
auto AddWork(Function &&function, Arguments &&... arguments) -> std::future< std::invoke_result_t< Function, Arguments... > >
int GetNumberOfCurrentlyIdleThreads() const
std::deque< std::function< void()> > m_WorkQueue
static ThreadPoolGlobals * m_PimplGlobals
void AddThreads(ThreadIdType count)
std::condition_variable m_Condition
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
unsigned int ThreadIdType