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
vtkMultiThreader::MultipleMethodExecute
void MultipleMethodExecute()
Execute the MultipleMethods (as define by calling SetMultipleMethod for each of the required this->Nu...
vtkMultiThreader::SetSingleMethod
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...
vtkMultiThreaderIDType
int vtkMultiThreaderIDType
Definition: vtkMultiThreader.h:73
vtkMultiThreader::GetGlobalMaximumNumberOfThreads
static int GetGlobalMaximumNumberOfThreads()
Set/Get the maximum number of threads to use when multithreading.
vtkMultiThreader::ThreadsEqual
static vtkTypeBool ThreadsEqual(vtkMultiThreaderIDType t1, vtkMultiThreaderIDType t2)
Check whether two thread identifiers refer to the same thread.
vtkMultiThreader::SetGlobalDefaultNumberOfThreads
static void SetGlobalDefaultNumberOfThreads(int val)
Set/Get the value which is used to initialize the NumberOfThreads in the constructor.
vtkX3D::data
@ data
Definition: vtkX3D.h:321
vtkMultiThreader::vtkMultiThreader
vtkMultiThreader()
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:82
vtkMultiThreader::SingleMethod
vtkThreadFunctionType SingleMethod
Definition: vtkMultiThreader.h:215
vtkMultiThreader::NumberOfThreads
int NumberOfThreads
Definition: vtkMultiThreader.h:207
vtkThreadProcessIDType
int vtkThreadProcessIDType
Definition: vtkMultiThreader.h:70
vtkMultiThreader::ThreadInfo::ActiveFlag
int * ActiveFlag
Definition: vtkMultiThreader.h:101
vtkMultiThreader::SingleData
void * SingleData
Definition: vtkMultiThreader.h:226
vtkMultiThreader::ThreadInfo::ThreadID
int ThreadID
Definition: vtkMultiThreader.h:99
vtkMultiThreader::ThreadInfo
This is the structure that is passed to the thread that is created from the SingleMethodExecute,...
Definition: vtkMultiThreader.h:97
vtkMultiThreader::TerminateThread
void TerminateThread(int threadId)
Terminate the thread that was created with a SpawnThreadExecute()
vtkMultiThreader
A class for performing multithreaded execution.
Definition: vtkMultiThreader.h:77
vtkMultiThreader::GetGlobalDefaultNumberOfThreads
static int GetGlobalDefaultNumberOfThreads()
Set/Get the value which is used to initialize the NumberOfThreads in the constructor.
vtkMultiThreader::SingleMethodExecute
void SingleMethodExecute()
Execute the SingleMethod (as define by SetSingleMethod) using this->NumberOfThreads threads.
vtkMultiThreader::ThreadInfo::NumberOfThreads
int NumberOfThreads
Definition: vtkMultiThreader.h:100
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:113
vtkMultiThreader::~vtkMultiThreader
~vtkMultiThreader() override
vtkMultiThreader::ThreadInfo::ActiveFlagLock
std::mutex * ActiveFlagLock
Definition: vtkMultiThreader.h:102
vtkMultiThreader::SetGlobalMaximumNumberOfThreads
static void SetGlobalMaximumNumberOfThreads(int val)
Set/Get the maximum number of threads to use when multithreading.
vtkObject.h
vtkMultiThreader::GetCurrentThreadID
static vtkMultiThreaderIDType GetCurrentThreadID()
Get the thread identifier of the calling thread.
vtkMultiThreader::PrintSelf
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkMultiThreader::New
static vtkMultiThreader * New()
vtkMultiThreader::GetNumberOfThreads
virtual int GetNumberOfThreads()
Get/Set the number of threads to create.
vtkMultiThreader::GetGlobalStaticMaximumNumberOfThreads
static int GetGlobalStaticMaximumNumberOfThreads()
Set/Get the maximum number of threads VTK was allocated to support.
vtkMultiThreader::IsThreadActive
vtkTypeBool IsThreadActive(int threadId)
Determine if a thread is still active.
vtkThreadFunctionType
void(* vtkThreadFunctionType)(void *)
Definition: vtkMultiThreader.h:69
vtkMultiThreader::ThreadInfo::UserData
void * UserData
Definition: vtkMultiThreader.h:103
vtkMultiThreader::SetMultipleMethod
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...
vtkX3D::index
@ index
Definition: vtkX3D.h:252
vtkTypeBool
int vtkTypeBool
Definition: vtkABI.h:69
vtkMultiThreader::SpawnThread
int SpawnThread(vtkThreadFunctionType, void *data)
Create a new thread for the given function.