VTK  9.2.6
vtkMultiThreader.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkMultiThreader.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
31 #ifndef vtkMultiThreader_h
32 #define vtkMultiThreader_h
33 
34 #include "vtkCommonCoreModule.h" // For export macro
35 #include "vtkObject.h"
36 #include "vtkThreads.h" // for VTK_MAX_THREADS
37 
38 #include <mutex> // For std::mutex
39 
40 #if defined(VTK_USE_PTHREADS)
41 #include <pthread.h> // Needed for PTHREAD implementation of mutex
42 #include <sys/types.h> // Needed for unix implementation of pthreads
43 #include <unistd.h> // Needed for unix implementation of pthreads
44 #endif
45 
46 // If VTK_USE_PTHREADS is defined, then pthread_create() will be
47 // used to create multiple threads
48 
49 // If VTK_USE_PTHREADS is defined, then the multithreaded
50 // function is of type void *, and returns nullptr
51 // Otherwise the type is void which is correct for WIN32
52 
53 // Defined in vtkThreads.h:
54 // VTK_MAX_THREADS
55 // VTK_THREAD_RETURN_VALUE
56 // VTK_THREAD_RETURN_TYPE
57 
58 #ifdef VTK_USE_PTHREADS
59 typedef void* (*vtkThreadFunctionType)(void*);
60 typedef pthread_t vtkThreadProcessIDType;
61 // #define VTK_THREAD_RETURN_VALUE nullptr
62 // #define VTK_THREAD_RETURN_TYPE void *
63 typedef pthread_t vtkMultiThreaderIDType;
64 #endif
65 
66 #ifdef VTK_USE_WIN32_THREADS
67 typedef vtkWindowsLPTHREAD_START_ROUTINE vtkThreadFunctionType;
68 typedef vtkWindowsHANDLE vtkThreadProcessIDType;
69 // #define VTK_THREAD_RETURN_VALUE 0
70 // #define VTK_THREAD_RETURN_TYPE DWORD __stdcall
71 typedef vtkWindowsDWORD vtkMultiThreaderIDType;
72 #endif
73 
74 #if !defined(VTK_USE_PTHREADS) && !defined(VTK_USE_WIN32_THREADS)
75 typedef void (*vtkThreadFunctionType)(void*);
77 // #define VTK_THREAD_RETURN_VALUE
78 // #define VTK_THREAD_RETURN_TYPE void
80 #endif
81 
82 class VTKCOMMONCORE_EXPORT vtkMultiThreader : public vtkObject
83 {
84 public:
85  static vtkMultiThreader* New();
86 
87  vtkTypeMacro(vtkMultiThreader, vtkObject);
88  void PrintSelf(ostream& os, vtkIndent indent) override;
89 
103  {
104  public:
105  int ThreadID;
108  std::mutex* ActiveFlagLock;
109  void* UserData;
110  };
111 
113 
118  vtkSetClampMacro(NumberOfThreads, int, 1, VTK_MAX_THREADS);
119  virtual int GetNumberOfThreads();
121 
123 
126  static int GetGlobalStaticMaximumNumberOfThreads();
128 
130 
135  static void SetGlobalMaximumNumberOfThreads(int val);
136  static int GetGlobalMaximumNumberOfThreads();
138 
140 
145  static void SetGlobalDefaultNumberOfThreads(int val);
146  static int GetGlobalDefaultNumberOfThreads();
148 
149  // These methods are excluded from wrapping 1) because the
150  // wrapper gives up on them and 2) because they really shouldn't be
151  // called from a script anyway.
152 
157  void SingleMethodExecute();
158 
164  void MultipleMethodExecute();
165 
173  void SetSingleMethod(vtkThreadFunctionType, void* data);
174 
179  void SetMultipleMethod(int index, vtkThreadFunctionType, void* data);
180 
186  int SpawnThread(vtkThreadFunctionType, void* data);
187 
191  void TerminateThread(int threadId);
192 
196  vtkTypeBool IsThreadActive(int threadId);
197 
201  static vtkMultiThreaderIDType GetCurrentThreadID();
202 
206  static vtkTypeBool ThreadsEqual(vtkMultiThreaderIDType t1, vtkMultiThreaderIDType t2);
207 
208 protected:
210  ~vtkMultiThreader() override;
211 
212  // The number of threads to use
214 
215  // An array of thread info containing a thread id
216  // (0, 1, 2, .. VTK_MAX_THREADS-1), the thread count, and a pointer
217  // to void so that user data can be passed to each thread
218  ThreadInfo ThreadInfoArray[VTK_MAX_THREADS];
219 
220  // The methods
222  vtkThreadFunctionType MultipleMethod[VTK_MAX_THREADS];
223 
224  // Storage of MutexFunctions and ints used to control spawned
225  // threads and the spawned thread ids
226  int SpawnedThreadActiveFlag[VTK_MAX_THREADS];
227  std::mutex* SpawnedThreadActiveFlagLock[VTK_MAX_THREADS];
228  vtkThreadProcessIDType SpawnedThreadProcessID[VTK_MAX_THREADS];
229  ThreadInfo SpawnedThreadInfoArray[VTK_MAX_THREADS];
230 
231  // Internal storage of the data
232  void* SingleData;
233  void* MultipleData[VTK_MAX_THREADS];
234 
235 private:
236  vtkMultiThreader(const vtkMultiThreader&) = delete;
237  void operator=(const vtkMultiThreader&) = delete;
238 };
239 
241 
242 #endif
abstract base class for most VTK objects
Definition: vtkObject.h:62
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
void(* vtkThreadFunctionType)(void *)
A class for performing multithreaded execution.
This is the structure that is passed to the thread that is created from the SingleMethodExecute, MultipleMethodExecute or the SpawnThread method.
int vtkMultiThreaderIDType
int vtkTypeBool
Definition: vtkABI.h:69
a simple class to control print indentation
Definition: vtkIndent.h:39
int vtkThreadProcessIDType
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
vtkThreadFunctionType SingleMethod