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 
35 namespace itk
36 {
37 
53 struct ThreadPoolGlobals;
54 
55 class ITKCommon_EXPORT ThreadPool : public Object
56 {
57 public:
58  ITK_DISALLOW_COPY_AND_MOVE(ThreadPool);
59 
61  using Self = ThreadPool;
62  using Superclass = Object;
65 
67  itkOverrideGetNameOfClassMacro(ThreadPool);
68 
70  static Pointer
71  New();
72 
74  static Pointer
75  GetInstance();
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
106  AddThreads(ThreadIdType count);
107 
110  {
111  const std::lock_guard<std::mutex> lockGuard(this->GetMutex());
112  return static_cast<ThreadIdType>(m_Threads.size());
113  }
114 
116  int
117  GetNumberOfCurrentlyIdleThreads() const;
118 
123  static bool
124  GetDoNotWaitForThreads();
125  static void
126  SetDoNotWaitForThreads(bool doNotWaitForThreads);
129 protected:
132  std::mutex &
133  GetMutex() const;
134 
135  ThreadPool();
136 
138  void
139  CleanUp();
140 
141  ~ThreadPool() override { this->CleanUp(); }
142 
143  static void
144  PrepareForFork();
145  static void
146  ResumeFromFork();
147 
148 private:
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
173  ThreadExecute();
174 };
175 
176 } // namespace itk
177 #endif
itkObjectFactory.h
itk::ThreadPool::m_Threads
std::vector< std::thread > m_Threads
Definition: itkThreadPool.h:163
itk::ThreadPool::~ThreadPool
~ThreadPool() override
Definition: itkThreadPool.h:141
itkSingletonMacro.h
itk::SmartPointer< Self >
itk::ThreadPool
Thread pool maintains a constant number of threads.
Definition: itkThreadPool.h:55
itk::ThreadPool::m_Condition
std::condition_variable m_Condition
Definition: itkThreadPool.h:159
itk::ThreadIdType
unsigned int ThreadIdType
Definition: itkIntTypes.h:102
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:55
itkIntTypes.h
itk::ThreadPool::GetMaximumNumberOfThreads
ThreadIdType GetMaximumNumberOfThreads() const
Definition: itkThreadPool.h:109
itkObject.h
itk::ThreadPool::m_WorkQueue
std::deque< std::function< void()> > m_WorkQueue
Definition: itkThreadPool.h:155
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
itk::ThreadPool::AddWork
auto AddWork(Function &&function, Arguments &&... arguments) -> std::future< std::invoke_result_t< Function, Arguments... >>
Definition: itkThreadPool.h:87
itkGetGlobalDeclarationMacro
#define itkGetGlobalDeclarationMacro(Type, VarName)
Definition: itkSingletonMacro.h:34
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:61
New
static Pointer New()
itk::ThreadPool::m_PimplGlobals
static ThreadPoolGlobals * m_PimplGlobals
Definition: itkThreadPool.h:169