VTK  9.0.1
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 =========================================================================*/
25 #ifndef vtkMultiThreader_h
26 #define vtkMultiThreader_h
27 
28 #include "vtkCommonCoreModule.h" // For export macro
29 #include "vtkObject.h"
30 
31 #include <mutex> // For std::mutex
32 
33 #if defined(VTK_USE_PTHREADS)
34 #include <pthread.h> // Needed for PTHREAD implementation of mutex
35 #include <sys/types.h> // Needed for unix implementation of pthreads
36 #include <unistd.h> // Needed for unix implementation of pthreads
37 #endif
38 
39 // If VTK_USE_PTHREADS is defined, then pthread_create() will be
40 // used to create multiple threads
41 
42 // Defined in vtkSystemIncludes.h:
43 // VTK_MAX_THREADS
44 
45 // If VTK_USE_PTHREADS is defined, then the multithreaded
46 // function is of type void *, and returns nullptr
47 // Otherwise the type is void which is correct for WIN32
48 
49 // Defined in vtkSystemIncludes.h:
50 // VTK_THREAD_RETURN_VALUE
51 // VTK_THREAD_RETURN_TYPE
52 
53 #ifdef VTK_USE_PTHREADS
54 typedef void* (*vtkThreadFunctionType)(void*);
55 typedef pthread_t vtkThreadProcessIDType;
56 // #define VTK_THREAD_RETURN_VALUE nullptr
57 // #define VTK_THREAD_RETURN_TYPE void *
58 typedef pthread_t vtkMultiThreaderIDType;
59 #endif
60 
61 #ifdef VTK_USE_WIN32_THREADS
62 typedef vtkWindowsLPTHREAD_START_ROUTINE vtkThreadFunctionType;
63 typedef vtkWindowsHANDLE vtkThreadProcessIDType;
64 // #define VTK_THREAD_RETURN_VALUE 0
65 // #define VTK_THREAD_RETURN_TYPE DWORD __stdcall
66 typedef vtkWindowsDWORD vtkMultiThreaderIDType;
67 #endif
68 
69 #if !defined(VTK_USE_PTHREADS) && !defined(VTK_USE_WIN32_THREADS)
70 typedef void (*vtkThreadFunctionType)(void*);
72 // #define VTK_THREAD_RETURN_VALUE
73 // #define VTK_THREAD_RETURN_TYPE void
75 #endif
76 
77 class VTKCOMMONCORE_EXPORT vtkMultiThreader : public vtkObject
78 {
79 public:
80  static vtkMultiThreader* New();
81 
82  vtkTypeMacro(vtkMultiThreader, vtkObject);
83  void PrintSelf(ostream& os, vtkIndent indent) override;
84 
97  class ThreadInfo
98  {
99  public:
100  int ThreadID;
103  std::mutex* ActiveFlagLock;
104  void* UserData;
105  };
106 
108 
113  vtkSetClampMacro(NumberOfThreads, int, 1, VTK_MAX_THREADS);
114  virtual int GetNumberOfThreads();
116 
118 
123  static void SetGlobalMaximumNumberOfThreads(int val);
124  static int GetGlobalMaximumNumberOfThreads();
126 
128 
133  static void SetGlobalDefaultNumberOfThreads(int val);
134  static int GetGlobalDefaultNumberOfThreads();
136 
137  // These methods are excluded from wrapping 1) because the
138  // wrapper gives up on them and 2) because they really shouldn't be
139  // called from a script anyway.
140 
145  void SingleMethodExecute();
146 
152  void MultipleMethodExecute();
153 
161  void SetSingleMethod(vtkThreadFunctionType, void* data);
162 
167  void SetMultipleMethod(int index, vtkThreadFunctionType, void* data);
168 
174  int SpawnThread(vtkThreadFunctionType, void* data);
175 
179  void TerminateThread(int thread_id);
180 
184  vtkTypeBool IsThreadActive(int threadID);
185 
189  static vtkMultiThreaderIDType GetCurrentThreadID();
190 
194  static vtkTypeBool ThreadsEqual(vtkMultiThreaderIDType t1, vtkMultiThreaderIDType t2);
195 
196 protected:
198  ~vtkMultiThreader() override;
199 
200  // The number of threads to use
202 
203  // An array of thread info containing a thread id
204  // (0, 1, 2, .. VTK_MAX_THREADS-1), the thread count, and a pointer
205  // to void so that user data can be passed to each thread
206  ThreadInfo ThreadInfoArray[VTK_MAX_THREADS];
207 
208  // The methods
210  vtkThreadFunctionType MultipleMethod[VTK_MAX_THREADS];
211 
212  // Storage of MutexFunctions and ints used to control spawned
213  // threads and the spawned thread ids
214  int SpawnedThreadActiveFlag[VTK_MAX_THREADS];
215  std::mutex* SpawnedThreadActiveFlagLock[VTK_MAX_THREADS];
216  vtkThreadProcessIDType SpawnedThreadProcessID[VTK_MAX_THREADS];
217  ThreadInfo SpawnedThreadInfoArray[VTK_MAX_THREADS];
218 
219  // Internal storage of the data
220  void* SingleData;
221  void* MultipleData[VTK_MAX_THREADS];
222 
223 private:
224  vtkMultiThreader(const vtkMultiThreader&) = delete;
225  void operator=(const vtkMultiThreader&) = delete;
226 };
227 
229 
230 #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:33
int vtkThreadProcessIDType
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
vtkThreadFunctionType SingleMethod