VTK  9.2.6
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=========================================================================*/
31#ifndef vtkMultiThreader_h
32#define vtkMultiThreader_h
33
34#include "vtkCommonCoreModule.h" // For export macro
35#include "vtkObject.h"
36#include "vtkThreads.h" // for VTK_MAX_THREADS
37
38#include <mutex> // For std::mutex
39
40#if defined(VTK_USE_PTHREADS)
41#include <pthread.h> // Needed for PTHREAD implementation of mutex
42#include <sys/types.h> // Needed for unix implementation of pthreads
43#include <unistd.h> // Needed for unix implementation of pthreads
44#endif
45
46// If VTK_USE_PTHREADS is defined, then pthread_create() will be
47// used to create multiple threads
48
49// If VTK_USE_PTHREADS is defined, then the multithreaded
50// function is of type void *, and returns nullptr
51// Otherwise the type is void which is correct for WIN32
52
53// Defined in vtkThreads.h:
54// VTK_MAX_THREADS
55// VTK_THREAD_RETURN_VALUE
56// VTK_THREAD_RETURN_TYPE
57
58#ifdef VTK_USE_PTHREADS
59typedef void* (*vtkThreadFunctionType)(void*);
60typedef pthread_t vtkThreadProcessIDType;
61// #define VTK_THREAD_RETURN_VALUE nullptr
62// #define VTK_THREAD_RETURN_TYPE void *
63typedef pthread_t vtkMultiThreaderIDType;
64#endif
65
66#ifdef VTK_USE_WIN32_THREADS
67typedef vtkWindowsLPTHREAD_START_ROUTINE vtkThreadFunctionType;
68typedef vtkWindowsHANDLE vtkThreadProcessIDType;
69// #define VTK_THREAD_RETURN_VALUE 0
70// #define VTK_THREAD_RETURN_TYPE DWORD __stdcall
71typedef vtkWindowsDWORD vtkMultiThreaderIDType;
72#endif
73
74#if !defined(VTK_USE_PTHREADS) && !defined(VTK_USE_WIN32_THREADS)
75typedef void (*vtkThreadFunctionType)(void*);
77// #define VTK_THREAD_RETURN_VALUE
78// #define VTK_THREAD_RETURN_TYPE void
80#endif
81
82class VTKCOMMONCORE_EXPORT vtkMultiThreader : public vtkObject
83{
84public:
86
88 void PrintSelf(ostream& os, vtkIndent indent) override;
89
103 {
104 public:
108 std::mutex* ActiveFlagLock;
109 void* UserData;
110 };
111
113
118 vtkSetClampMacro(NumberOfThreads, int, 1, VTK_MAX_THREADS);
119 virtual int GetNumberOfThreads();
121
123
128
130
138
140
148
149 // These methods are excluded from wrapping 1) because the
150 // wrapper gives up on them and 2) because they really shouldn't be
151 // called from a script anyway.
152
158
165
174
179 void SetMultipleMethod(int index, vtkThreadFunctionType, void* data);
180
187
191 void TerminateThread(int threadId);
192
197
202
207
208protected:
211
212 // The number of threads to use
214
215 // An array of thread info containing a thread id
216 // (0, 1, 2, .. VTK_MAX_THREADS-1), the thread count, and a pointer
217 // to void so that user data can be passed to each thread
218 ThreadInfo ThreadInfoArray[VTK_MAX_THREADS];
219
220 // The methods
222 vtkThreadFunctionType MultipleMethod[VTK_MAX_THREADS];
223
224 // Storage of MutexFunctions and ints used to control spawned
225 // threads and the spawned thread ids
226 int SpawnedThreadActiveFlag[VTK_MAX_THREADS];
227 std::mutex* SpawnedThreadActiveFlagLock[VTK_MAX_THREADS];
228 vtkThreadProcessIDType SpawnedThreadProcessID[VTK_MAX_THREADS];
229 ThreadInfo SpawnedThreadInfoArray[VTK_MAX_THREADS];
230
231 // Internal storage of the data
233 void* MultipleData[VTK_MAX_THREADS];
234
235private:
236 vtkMultiThreader(const vtkMultiThreader&) = delete;
237 void operator=(const vtkMultiThreader&) = delete;
238};
239
241
242#endif
a simple class to control print indentation
Definition: vtkIndent.h:40
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.
void TerminateThread(int threadId)
Terminate the thread that was created with a SpawnThreadExecute()
static vtkMultiThreader * New()
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
int vtkTypeBool
Definition: vtkABI.h:69
int vtkMultiThreaderIDType
int vtkThreadProcessIDType
void(* vtkThreadFunctionType)(void *)