ITK  5.2.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  * http://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  itkTypeMacro(ThreadPool, Object);
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)
88  -> std::future<typename std::result_of<Function(Arguments...)>::type>
89  {
90  using return_type = typename std::result_of<Function(Arguments...)>::type;
92 
93  auto task = std::make_shared<std::packaged_task<return_type()>>(
94  std::bind(std::forward<Function>(function), std::forward<Arguments>(arguments)...));
95 
96  std::future<return_type> res = task->get_future();
97  {
98  std::unique_lock<std::mutex> lock(this->GetMutex());
99  m_WorkQueue.emplace_back([task]() { (*task)(); });
100  }
101  m_Condition.notify_one();
102  return res;
103  }
104 
106  void
107  AddThreads(ThreadIdType count);
108 
111  {
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);
128 
129 protected:
130  /* We need access to the mutex in AddWork, and the variable is only
131  * visible in .cxx file, so this method returns it. */
132  std::mutex &
133  GetMutex();
134 
135  ThreadPool();
136  ~ThreadPool() override;
137 
138 private:
140  itkGetGlobalDeclarationMacro(ThreadPoolGlobals, PimplGlobals);
141 
145  std::deque<std::function<void()>> m_WorkQueue;
146 
149  std::condition_variable m_Condition;
150 
153  std::vector<std::thread> m_Threads;
154 
155  /* Has destruction started? */
156  bool m_Stopping{ false };
157 
159  static ThreadPoolGlobals * m_PimplGlobals;
160 
162  static void
163  ThreadExecute();
164 };
165 
166 } // namespace itk
167 #endif
itkObjectFactory.h
itk::ThreadPool::m_Threads
std::vector< std::thread > m_Threads
Definition: itkThreadPool.h:153
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:149
itk::ThreadIdType
unsigned int ThreadIdType
Definition: itkIntTypes.h:99
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:59
itkIntTypes.h
itk::ThreadPool::GetMaximumNumberOfThreads
ThreadIdType GetMaximumNumberOfThreads() const
Definition: itkThreadPool.h:110
itkObject.h
itk::ThreadPool::m_WorkQueue
std::deque< std::function< void()> > m_WorkQueue
Definition: itkThreadPool.h:145
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::ThreadPool::AddWork
auto AddWork(Function &&function, Arguments &&... arguments) -> std::future< typename std::result_of< Function(Arguments...)>::type >
Definition: itkThreadPool.h:87
itkGetGlobalDeclarationMacro
#define itkGetGlobalDeclarationMacro(Type, VarName)
Definition: itkSingletonMacro.h:35
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:62
itk::ThreadPool::m_PimplGlobals
static ThreadPoolGlobals * m_PimplGlobals
Definition: itkThreadPool.h:159