VTK  9.1.0
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 #include "vtkThreads.h" // for VTK_MAX_THREADS
31 
32 #include <mutex> // For std::mutex
33 
34 #if defined(VTK_USE_PTHREADS)
35 #include <pthread.h> // Needed for PTHREAD implementation of mutex
36 #include <sys/types.h> // Needed for unix implementation of pthreads
37 #include <unistd.h> // Needed for unix implementation of pthreads
38 #endif
39 
40 // If VTK_USE_PTHREADS is defined, then pthread_create() will be
41 // used to create multiple threads
42 
43 // If VTK_USE_PTHREADS is defined, then the multithreaded
44 // function is of type void *, and returns nullptr
45 // Otherwise the type is void which is correct for WIN32
46 
47 // Defined in vtkThreads.h:
48 // VTK_MAX_THREADS
49 // VTK_THREAD_RETURN_VALUE
50 // VTK_THREAD_RETURN_TYPE
51 
52 #ifdef VTK_USE_PTHREADS
53 typedef void* (*vtkThreadFunctionType)(void*);
54 typedef pthread_t vtkThreadProcessIDType;
55 // #define VTK_THREAD_RETURN_VALUE nullptr
56 // #define VTK_THREAD_RETURN_TYPE void *
57 typedef pthread_t vtkMultiThreaderIDType;
58 #endif
59 
60 #ifdef VTK_USE_WIN32_THREADS
61 typedef vtkWindowsLPTHREAD_START_ROUTINE vtkThreadFunctionType;
62 typedef vtkWindowsHANDLE vtkThreadProcessIDType;
63 // #define VTK_THREAD_RETURN_VALUE 0
64 // #define VTK_THREAD_RETURN_TYPE DWORD __stdcall
65 typedef vtkWindowsDWORD vtkMultiThreaderIDType;
66 #endif
67 
68 #if !defined(VTK_USE_PTHREADS) && !defined(VTK_USE_WIN32_THREADS)
69 typedef void (*vtkThreadFunctionType)(void*);
71 // #define VTK_THREAD_RETURN_VALUE
72 // #define VTK_THREAD_RETURN_TYPE void
74 #endif
75 
76 class VTKCOMMONCORE_EXPORT vtkMultiThreader : public vtkObject
77 {
78 public:
79  static vtkMultiThreader* New();
80 
81  vtkTypeMacro(vtkMultiThreader, vtkObject);
82  void PrintSelf(ostream& os, vtkIndent indent) override;
83 
96  class ThreadInfo
97  {
98  public:
99  int ThreadID;
102  std::mutex* ActiveFlagLock;
103  void* UserData;
104  };
105 
107 
112  vtkSetClampMacro(NumberOfThreads, int, 1, VTK_MAX_THREADS);
113  virtual int GetNumberOfThreads();
115 
117 
122 
124 
129  static void SetGlobalMaximumNumberOfThreads(int val);
132 
134 
139  static void SetGlobalDefaultNumberOfThreads(int val);
142 
143  // These methods are excluded from wrapping 1) because the
144  // wrapper gives up on them and 2) because they really shouldn't be
145  // called from a script anyway.
146 
152 
159 
168 
174 
181 
185  void TerminateThread(int threadId);
186 
191 
196 
201 
202 protected:
204  ~vtkMultiThreader() override;
205 
206  // The number of threads to use
208 
209  // An array of thread info containing a thread id
210  // (0, 1, 2, .. VTK_MAX_THREADS-1), the thread count, and a pointer
211  // to void so that user data can be passed to each thread
212  ThreadInfo ThreadInfoArray[VTK_MAX_THREADS];
213 
214  // The methods
216  vtkThreadFunctionType MultipleMethod[VTK_MAX_THREADS];
217 
218  // Storage of MutexFunctions and ints used to control spawned
219  // threads and the spawned thread ids
220  int SpawnedThreadActiveFlag[VTK_MAX_THREADS];
221  std::mutex* SpawnedThreadActiveFlagLock[VTK_MAX_THREADS];
222  vtkThreadProcessIDType SpawnedThreadProcessID[VTK_MAX_THREADS];
223  ThreadInfo SpawnedThreadInfoArray[VTK_MAX_THREADS];
224 
225  // Internal storage of the data
226  void* SingleData;
227  void* MultipleData[VTK_MAX_THREADS];
228 
229 private:
230  vtkMultiThreader(const vtkMultiThreader&) = delete;
231  void operator=(const vtkMultiThreader&) = delete;
232 };
233 
235 
236 #endif
a simple class to control print indentation
Definition: vtkIndent.h:34
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:63
@ index
Definition: vtkX3D.h:252
@ data
Definition: vtkX3D.h:321
int vtkTypeBool
Definition: vtkABI.h:69
int vtkMultiThreaderIDType
int vtkThreadProcessIDType
void(* vtkThreadFunctionType)(void *)