VTK
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
37#ifdef VTK_USE_SPROC
38#include <sys/types.h> // Needed for unix implementation of sproc
39#include <unistd.h> // Needed for unix implementation of sproc
40#endif
41
42#if defined(VTK_USE_PTHREADS) || defined(VTK_HP_PTHREADS)
43#include <pthread.h> // Needed for PTHREAD implementation of mutex
44#include <sys/types.h> // Needed for unix implementation of pthreads
45#include <unistd.h> // Needed for unix implementation of pthreads
46#endif
47
48// If VTK_USE_SPROC is defined, then sproc() will be used to create
49// multiple threads on an SGI. If VTK_USE_PTHREADS is defined, then
50// pthread_create() will be used to create multiple threads (on
51// a sun, for example)
52
53// Defined in vtkSystemIncludes.h:
54// VTK_MAX_THREADS
55
56// If VTK_USE_PTHREADS is defined, then the multithreaded
57// function is of type void *, and returns NULL
58// Otherwise the type is void which is correct for WIN32
59// and SPROC
60
61#ifdef VTK_USE_SPROC
62typedef int vtkThreadProcessIDType;
63typedef int vtkMultiThreaderIDType;
64#endif
65
66// Defined in vtkSystemIncludes.h:
67// VTK_THREAD_RETURN_VALUE
68// VTK_THREAD_RETURN_TYPE
69
70#ifdef VTK_USE_PTHREADS
71typedef void *(*vtkThreadFunctionType)(void *);
72typedef pthread_t vtkThreadProcessIDType;
73// #define VTK_THREAD_RETURN_VALUE NULL
74// #define VTK_THREAD_RETURN_TYPE void *
75typedef pthread_t vtkMultiThreaderIDType;
76#endif
77
78#ifdef VTK_USE_WIN32_THREADS
79typedef vtkWindowsLPTHREAD_START_ROUTINE vtkThreadFunctionType;
80typedef vtkWindowsHANDLE vtkThreadProcessIDType;
81// #define VTK_THREAD_RETURN_VALUE 0
82// #define VTK_THREAD_RETURN_TYPE DWORD __stdcall
83typedef vtkWindowsDWORD vtkMultiThreaderIDType;
84#endif
85
86#if !defined(VTK_USE_PTHREADS) && !defined(VTK_USE_WIN32_THREADS)
87typedef void (*vtkThreadFunctionType)(void *);
89// #define VTK_THREAD_RETURN_VALUE
90// #define VTK_THREAD_RETURN_TYPE void
92#endif
93
94class vtkMutexLock;
95
96class VTKCOMMONCORE_EXPORT vtkMultiThreader : public vtkObject
97{
98public:
100
102 void PrintSelf(ostream& os, vtkIndent indent) VTK_OVERRIDE;
103
117#define ThreadInfoStruct vtkMultiThreader::ThreadInfo
119 {
120 public:
125 void *UserData;
126 };
127
129
134 vtkSetClampMacro( NumberOfThreads, int, 1, VTK_MAX_THREADS );
135 virtual int GetNumberOfThreads();
137
139
147
149
157
158 // These methods are excluded from Tcl wrapping 1) because the
159 // wrapper gives up on them and 2) because they really shouldn't be
160 // called from a script anyway.
161
167
174
183
189
196
200 void TerminateThread( int thread_id );
201
205 int IsThreadActive( int threadID );
206
211
217
218protected:
220 ~vtkMultiThreader() VTK_OVERRIDE;
221
222 // The number of threads to use
223 int NumberOfThreads;
224
225 // An array of thread info containing a thread id
226 // (0, 1, 2, .. VTK_MAX_THREADS-1), the thread count, and a pointer
227 // to void so that user data can be passed to each thread
228 ThreadInfo ThreadInfoArray[VTK_MAX_THREADS];
229
230 // The methods
232 vtkThreadFunctionType MultipleMethod[VTK_MAX_THREADS];
233
234 // Storage of MutexFunctions and ints used to control spawned
235 // threads and the spawned thread ids
236 int SpawnedThreadActiveFlag[VTK_MAX_THREADS];
237 vtkMutexLock *SpawnedThreadActiveFlagLock[VTK_MAX_THREADS];
238 vtkThreadProcessIDType SpawnedThreadProcessID[VTK_MAX_THREADS];
239 ThreadInfo SpawnedThreadInfoArray[VTK_MAX_THREADS];
240
241 // Internal storage of the data
242 void *SingleData;
243 void *MultipleData[VTK_MAX_THREADS];
244
245private:
246 vtkMultiThreader(const vtkMultiThreader&) VTK_DELETE_FUNCTION;
247 void operator=(const vtkMultiThreader&) VTK_DELETE_FUNCTION;
248};
249
250#endif
251
252
253
254
255
a simple class to control print indentation
Definition: vtkIndent.h:40
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.
~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.
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 ThreadsEqual(vtkMultiThreaderIDType t1, vtkMultiThreaderIDType t2)
Check whether two thread identifiers refer to the same thread.
int IsThreadActive(int threadID)
Determine if a thread is still active.
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()
mutual exclusion locking class
Definition: vtkMutexLock.h:88
abstract base class for most VTK objects
Definition: vtkObject.h:60
@ index
Definition: vtkX3D.h:246
@ data
Definition: vtkX3D.h:315
int vtkMultiThreaderIDType
int vtkThreadProcessIDType
void(* vtkThreadFunctionType)(void *)