ITK  6.0.0
Insight Toolkit
itkThreadPool.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18#ifndef itkThreadPool_h
19#define itkThreadPool_h
20
21#include "itkConfigure.h"
22#include "itkIntTypes.h"
23
24#include <deque>
25#include <functional>
26#include <future>
27#include <condition_variable>
28#include <thread>
29
30#include "itkObject.h"
31#include "itkObjectFactory.h"
32#include "itkSingletonMacro.h"
33
34
35namespace itk
36{
37
53struct ThreadPoolGlobals;
54
55class ITKCommon_EXPORT ThreadPool : public Object
56{
57public:
58 ITK_DISALLOW_COPY_AND_MOVE(ThreadPool);
59
65
67 itkOverrideGetNameOfClassMacro(ThreadPool);
68
70 static Pointer
71 New();
72
74 static Pointer
76
85 template <class Function, class... Arguments>
86 auto
87 AddWork(Function && function, Arguments &&... arguments) -> std::future<std::invoke_result_t<Function, Arguments...>>
88 {
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...); });
94
95 std::future<return_type> res = task->get_future();
96 {
97 const std::lock_guard<std::mutex> lockGuard(this->GetMutex());
98 m_WorkQueue.emplace_back([task]() { (*task)(); });
99 }
100 m_Condition.notify_one();
101 return res;
102 }
103
105 void
107
110 {
111 const std::lock_guard<std::mutex> lockGuard(this->GetMutex());
112 return static_cast<ThreadIdType>(m_Threads.size());
113 }
114
116 int
118
123 static bool
125 static void
126 SetDoNotWaitForThreads(bool doNotWaitForThreads);
129protected:
132 std::mutex &
133 GetMutex() const;
134
136
138 void
140
141 ~ThreadPool() override { this->CleanUp(); }
142
143 static void
145 static void
147
148private:
150 itkGetGlobalDeclarationMacro(ThreadPoolGlobals, PimplGlobals);
151
155 std::deque<std::function<void()>> m_WorkQueue; // guarded by m_PimplGlobals->m_Mutex
156
159 std::condition_variable m_Condition;
160
163 std::vector<std::thread> m_Threads; // guarded by m_PimplGlobals->m_Mutex
164
165 /* Has destruction started? */
166 bool m_Stopping{ false }; // guarded by m_PimplGlobals->m_Mutex
167
169 static ThreadPoolGlobals * m_PimplGlobals;
170
172 static void
174};
175
176} // namespace itk
177#endif
Light weight base class for most itk classes.
Base class for most ITK classes.
Definition: itkObject.h:62
Thread pool maintains a constant number of threads.
Definition: itkThreadPool.h:56
static Pointer New()
itkGetGlobalDeclarationMacro(ThreadPoolGlobals, PimplGlobals)
static void ThreadExecute()
~ThreadPool() override
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... > >
Definition: itkThreadPool.h:87
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
Definition: itkIntTypes.h:102