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);
126 
128 
133  static void SetGlobalDefaultNumberOfThreads(int val);
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 
146 
153 
162 
168 
175 
179  void TerminateThread(int thread_id);
180 
185 
190 
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
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()
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.
vtkTypeBool IsThreadActive(int threadID)
Determine if a thread is still active.
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...
void MultipleMethodExecute()
Execute the MultipleMethods (as define by calling SetMultipleMethod for each of the required this->Nu...
static vtkMultiThreader * New()
void TerminateThread(int thread_id)
Terminate the thread that was created with a SpawnThreadExecute()
static vtkMultiThreaderIDType GetCurrentThreadID()
Get the thread identifier of the calling thread.
static int GetGlobalDefaultNumberOfThreads()
virtual int GetNumberOfThreads()
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 *)