ITK  4.13.0
Insight Segmentation and Registration Toolkit
itkThreadPool.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
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 <map>
25 #include <set>
26 #include <deque>
27 
28 #include "itkThreadJob.h"
29 #include "itkObject.h"
30 #include "itkObjectFactory.h"
31 #include "itkSimpleFastMutexLock.h"
32 #include "itkMutexLockHolder.h"
33 
34 namespace itk
35 {
36 
61 class ITKCommon_EXPORT ThreadPool : public Object
62 {
63 public:
64 
66  typedef ThreadPool Self;
67  typedef Object Superclass;
70 
71  typedef ThreadJob::Semaphore Semaphore;
72 
74  itkTypeMacro(ThreadPool, Object);
75 
77  static Pointer New();
78 
80  static Pointer GetInstance();
81 
88  void AddWork(const ThreadJob& job);
89 
91  void AddThreads(ThreadIdType count);
92 
94  int GetNumberOfCurrentlyIdleThreads() const;
95 
97  void WaitForJob(Semaphore& jobSemaphore);
98 
100  static ThreadIdType GetGlobalDefaultNumberOfThreadsByPlatform();
101 
103  static ThreadIdType GetGlobalDefaultNumberOfThreads();
104 
105 protected:
106 
107  static void PlatformCreate(Semaphore &semaphore);
108  static void PlatformWait(Semaphore &semaphore);
109  static void PlatformSignal(Semaphore &semaphore);
110  static void PlatformDelete(Semaphore &semaphore);
111  static bool PlatformClose(ThreadProcessIdType &threadId); //returns success status
112 
117  void AddThread();
118 
120  void DeleteThreads();
121 
122  ThreadPool();
123  virtual ~ThreadPool() ITK_OVERRIDE;
124 
125 private:
126  ITK_DISALLOW_COPY_AND_ASSIGN(ThreadPool);
127 
129  bool m_ExceptionOccurred;
130 
135  std::deque<ThreadJob> m_WorkQueue;
136 
140  Semaphore m_ThreadsSemaphore;
141 
145  std::vector<ThreadProcessIdType> m_Threads;
146 
148  static SimpleFastMutexLock m_Mutex;
149 
150  static Pointer m_ThreadPoolInstance;
151 
153  static ITK_THREAD_RETURN_TYPE ThreadExecute(void *param);
154 };
155 
156 }
157 #endif
Critical section locking class that can be allocated on the stack.
ThreadPool Self
Definition: itkThreadPool.h:66
This class is used to submit jobs to the thread pool.
Definition: itkThreadJob.h:56
SmartPointer< Self > Pointer
Definition: itkThreadPool.h:68
#define ITK_THREAD_RETURN_TYPE
SmartPointer< const Self > ConstPointer
Definition: itkThreadPool.h:69
unsigned int ThreadIdType
Definition: itkIntTypes.h:159
int ThreadProcessIdType
Thread pool maintains a constant number of threads.
Definition: itkThreadPool.h:61
Base class for most ITK classes.
Definition: itkObject.h:59
ThreadJob::Semaphore Semaphore
Definition: itkThreadPool.h:71