VTK  9.3.20240418
vtkMultiThreader.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
19 #ifndef vtkMultiThreader_h
20 #define vtkMultiThreader_h
21 
22 #include "vtkCommonCoreModule.h" // For export macro
23 #include "vtkObject.h"
24 #include "vtkThreads.h" // for VTK_MAX_THREADS
25 
26 #include <mutex> // For std::mutex
27 
28 #if defined(VTK_USE_PTHREADS)
29 #include <pthread.h> // Needed for PTHREAD implementation of mutex
30 #include <sys/types.h> // Needed for unix implementation of pthreads
31 #include <unistd.h> // Needed for unix implementation of pthreads
32 #endif
33 
34 // If VTK_USE_PTHREADS is defined, then pthread_create() will be
35 // used to create multiple threads
36 
37 // If VTK_USE_PTHREADS is defined, then the multithreaded
38 // function is of type void *, and returns nullptr
39 // Otherwise the type is void which is correct for WIN32
40 
41 // Defined in vtkThreads.h:
42 // VTK_MAX_THREADS
43 // VTK_THREAD_RETURN_VALUE
44 // VTK_THREAD_RETURN_TYPE
45 
46 #ifdef VTK_USE_PTHREADS
47 typedef void* (*vtkThreadFunctionType)(void*);
48 typedef pthread_t vtkThreadProcessIDType;
49 // #define VTK_THREAD_RETURN_VALUE nullptr
50 // #define VTK_THREAD_RETURN_TYPE void *
51 typedef pthread_t vtkMultiThreaderIDType;
52 #endif
53 
54 #ifdef VTK_USE_WIN32_THREADS
55 typedef vtkWindowsLPTHREAD_START_ROUTINE vtkThreadFunctionType;
56 typedef vtkWindowsHANDLE vtkThreadProcessIDType;
57 // #define VTK_THREAD_RETURN_VALUE 0
58 // #define VTK_THREAD_RETURN_TYPE DWORD __stdcall
59 typedef vtkWindowsDWORD vtkMultiThreaderIDType;
60 #endif
61 
62 #if !defined(VTK_USE_PTHREADS) && !defined(VTK_USE_WIN32_THREADS)
63 typedef void (*vtkThreadFunctionType)(void*);
65 // #define VTK_THREAD_RETURN_VALUE
66 // #define VTK_THREAD_RETURN_TYPE void
68 #endif
69 
70 VTK_ABI_NAMESPACE_BEGIN
71 class VTKCOMMONCORE_EXPORT vtkMultiThreader : public vtkObject
72 {
73 public:
74  static vtkMultiThreader* New();
75 
76  vtkTypeMacro(vtkMultiThreader, vtkObject);
77  void PrintSelf(ostream& os, vtkIndent indent) override;
78 
91  class ThreadInfo
92  {
93  public:
94  int ThreadID;
96  int* ActiveFlag;
97  std::mutex* ActiveFlagLock;
98  void* UserData;
99  };
100 
102 
107  vtkSetClampMacro(NumberOfThreads, int, 1, VTK_MAX_THREADS);
108  virtual int GetNumberOfThreads();
110 
112 
117 
119 
124  static void SetGlobalMaximumNumberOfThreads(int val);
127 
129 
134  static void SetGlobalDefaultNumberOfThreads(int val);
137 
138  // These methods are excluded from wrapping 1) because the
139  // wrapper gives up on them and 2) because they really shouldn't be
140  // called from a script anyway.
141 
147 
154 
163 
169 
176 
180  void TerminateThread(int threadId);
181 
186 
191 
196 
197 protected:
199  ~vtkMultiThreader() override;
200 
201  // The number of threads to use
203 
204  // An array of thread info containing a thread id
205  // (0, 1, 2, .. VTK_MAX_THREADS-1), the thread count, and a pointer
206  // to void so that user data can be passed to each thread
207  ThreadInfo ThreadInfoArray[VTK_MAX_THREADS];
208 
209  // The methods
211  vtkThreadFunctionType MultipleMethod[VTK_MAX_THREADS];
212 
213  // Storage of MutexFunctions and ints used to control spawned
214  // threads and the spawned thread ids
215  int SpawnedThreadActiveFlag[VTK_MAX_THREADS];
216  std::mutex* SpawnedThreadActiveFlagLock[VTK_MAX_THREADS];
217  vtkThreadProcessIDType SpawnedThreadProcessID[VTK_MAX_THREADS];
218  ThreadInfo SpawnedThreadInfoArray[VTK_MAX_THREADS];
219 
220  // Internal storage of the data
221  void* SingleData;
222  void* MultipleData[VTK_MAX_THREADS];
223 
224 private:
225  vtkMultiThreader(const vtkMultiThreader&) = delete;
226  void operator=(const vtkMultiThreader&) = delete;
227 };
228 
230 
231 VTK_ABI_NAMESPACE_END
232 #endif
a simple class to control print indentation
Definition: vtkIndent.h:108
This is the structure that is passed to the thread that is created from the SingleMethodExecute,...
A class for performing multithreaded execution.
static void SetGlobalDefaultNumberOfThreads(int val)
Set/Get the value which is used to initialize the NumberOfThreads in the constructor.
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
static int GetGlobalMaximumNumberOfThreads()
Set/Get the maximum number of threads to use when multithreading.
int SpawnThread(vtkThreadFunctionType, void *data)
Create a new thread for the given function.
vtkThreadFunctionType SingleMethod
~vtkMultiThreader() override
static void SetGlobalMaximumNumberOfThreads(int val)
Set/Get the maximum number of threads to use when multithreading.
void SetSingleMethod(vtkThreadFunctionType, void *data)
Set the SingleMethod to f() and the UserData field of the ThreadInfo that is passed to it will be dat...
void SingleMethodExecute()
Execute the SingleMethod (as define by SetSingleMethod) using this->NumberOfThreads threads.
static vtkTypeBool ThreadsEqual(vtkMultiThreaderIDType t1, vtkMultiThreaderIDType t2)
Check whether two thread identifiers refer to the same thread.
void SetMultipleMethod(int index, vtkThreadFunctionType, void *data)
Set the MultipleMethod at the given index to f() and the UserData field of the ThreadInfo that is pas...
static int GetGlobalStaticMaximumNumberOfThreads()
Set/Get the maximum number of threads VTK was allocated to support.
void MultipleMethodExecute()
Execute the MultipleMethods (as define by calling SetMultipleMethod for each of the required this->Nu...
vtkTypeBool IsThreadActive(int threadId)
Determine if a thread is still active.
static vtkMultiThreader * New()
void TerminateThread(int threadId)
Terminate the thread that was created with a SpawnThreadExecute()
static vtkMultiThreaderIDType GetCurrentThreadID()
Get the thread identifier of the calling thread.
static int GetGlobalDefaultNumberOfThreads()
Set/Get the value which is used to initialize the NumberOfThreads in the constructor.
virtual int GetNumberOfThreads()
Get/Set the number of threads to create.
abstract base class for most VTK objects
Definition: vtkObject.h:162
@ index
Definition: vtkX3D.h:246
@ data
Definition: vtkX3D.h:315
int vtkTypeBool
Definition: vtkABI.h:64
int vtkMultiThreaderIDType
int vtkThreadProcessIDType
void(* vtkThreadFunctionType)(void *)