Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  
igtlMultiThreader.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: The OpenIGTLink Library
4 Language: C++
5 Web page: http://openigtlink.org/
6
7 Copyright (c) Insight Software Consortium. All rights reserved.
8
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the above copyright notices for more information.
12
13=========================================================================*/
14/*=========================================================================
15
16 Program: Visualization Toolkit
17 Module: $RCSfile: vtkMultiThreader.h,v $
18
19 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
20 All rights reserved.
21 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
22
23 This software is distributed WITHOUT ANY WARRANTY; without even
24 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
25 PURPOSE. See the above copyright notice for more information.
26
27=========================================================================*/
28// .NAME igtlMultiThreader - A class for performing multithreaded execution
29// .SECTION Description
30// igtlMultithreader is a class that provides support for multithreaded
31// execution using sproc() on an SGI, or pthread_create on any platform
32// supporting POSIX threads. This class can be used to execute a single
33// method on multiple threads, or to specify a method per thread.
34
35#ifndef __igtlMultiThreader_h
36#define __igtlMultiThreader_h
37
38#include "igtlObject.h"
39#include "igtlObjectFactory.h"
40#include "igtlMacro.h"
41#include "igtlMutexLock.h"
42
43
44#ifdef OpenIGTLink_USE_SPROC
45#include <sys/types.h> // Needed for unix implementation of sproc
46#include <unistd.h> // Needed for unix implementation of sproc
47#endif
48
49#if defined(OpenIGTLink_USE_PTHREAD) || defined(OpenIGTLink_HP_PTHREAD)
50#include <pthread.h> // Needed for PTHREAD implementation of mutex
51#include <sys/types.h> // Needed for unix implementation of pthreads
52#include <unistd.h> // Needed for unix implementation of pthreads
53#endif
54
55namespace igtl
56{
57
58// If OpenIGTLink_USE_SPROC is defined, then sproc() will be used to create
59// multiple threads on an SGI. If OpenIGTLink_USE_PTHREAD is defined, then
60// pthread_create() will be used to create multiple threads (on
61// a sun, for example)
62
63// Defined in igtlSystemIncludes.h:
64// IGTL_MAX_THREADS
65
66// If OpenIGTLink_USE_PTHREADS is defined, then the multithreaded
67// function is of type void *, and returns NULL
68// Otherwise the type is void which is correct for WIN32
69// and SPROC
70//BTX
71
72// The maximum number of threads allowed
73#ifdef OpenIGTLink_USE_SPROC
74#define IGTL_MAX_THREADS 128
75#endif
76
77#ifdef OpenIGTLink_USE_PTHREADS
78#define IGTL_MAX_THREADS 128
79#endif
80
81#ifdef OpenIGTLink_USE_WIN32_THREADS
82#define IGTL_MAX_THREADS 128
83#endif
84
85// cygwin threads are unreliable
86#ifdef __CYGWIN__
87#undef IGTL_MAX_THREADS
88#define IGTL_MAX_THREADS 128
89#endif
90
91// mingw threads cause crashes so limit to 1
92#if defined(__MINGW32__)
93#undef IGTL_MAX_THREADS
94#define IGTL_MAX_THREADS 1
95#endif
96
97// On some sgi machines, threads and stl don't mix so limit to 1
98#if defined(__sgi) && defined(_COMPILER_VERSION) && _COMPILER_VERSION <= 730
99#undef IGTL_MAX_THREADS
100#define IGTL_MAX_THREADS 1
101#endif
102
103#ifndef IGTL_MAX_THREADS
104#define IGTL_MAX_THREADS 1
105#endif
106
107#ifdef OpenIGTLink_USE_SPROC
108typedef int ThreadProcessIDType;
109typedef int MultiThreaderIDType;
110#endif
111
112#ifdef OpenIGTLink_USE_PTHREADS
113typedef void *(*ThreadFunctionType)(void *);
114typedef pthread_t ThreadProcessIDType;
115typedef pthread_t MultiThreaderIDType;
116#endif
117
118#ifdef OpenIGTLink_USE_WIN32_THREADS
119typedef igtlWindowsLPTHREAD_START_ROUTINE ThreadFunctionType;
120typedef igtlWindowsHANDLE ThreadProcessIDType;
121typedef igtlWindowsDWORD MultiThreaderIDType;
122#endif
123
124#if !defined(OpenIGTLink_USE_PTHREADS) && !defined(OpenIGTLink_USE_WIN32_THREADS)
125typedef void (*ThreadFunctionType)(void *);
128#endif
129//ETX
130
131
133{
134public:
140
143
144 // Description:
145 // This is the structure that is passed to the thread that is
146 // created from the SingleMethodExecute, MultipleMethodExecute or
147 // the SpawnThread method. It is passed in as a void *, and it is
148 // up to the method to cast correctly and extract the information.
149 // The ThreadID is a number between 0 and NumberOfThreads-1 that indicates
150 // the id of this thread. The NumberOfThreads is this->NumberOfThreads for
151 // threads created from SingleMethodExecute or MultipleMethodExecute,
152 // and it is 1 for threads created from SpawnThread.
153 // The UserData is the (void *)arg passed into the SetSingleMethod,
154 // SetMultipleMethod, or SpawnThread method.
155
156 //BTX
157#define ThreadInfoStruct MultiThreader::ThreadInfo
167 //ETX
168
169 // Description:
170 // Get/Set the number of threads to create. It will be clamped to the range
171 // 1 - IGTL_MAX_THREADS, so the caller of this method should check that the
172 // requested number of threads was accepted.
173 igtlSetClampMacro( NumberOfThreads, int, 1, IGTL_MAX_THREADS );
174 virtual int GetNumberOfThreads();
175
176 // Description:
177 // Set/Get the maximum number of threads to use when multithreading.
178 // This limits and overrides any other settings for multithreading.
179 // A value of zero indicates no limit.
182
183 // Description:
184 // Set/Get the value which is used to initialize the NumberOfThreads
185 // in the constructor. Initially this default is set to the number of
186 // processors or IGTL_MAX_THREADS (which ever is less).
189
190 // These methods are excluded from Tcl wrapping 1) because the
191 // wrapper gives up on them and 2) because they really shouldn't be
192 // called from a script anyway.
193 //BTX
194
195 // Description:
196 // Execute the SingleMethod (as define by SetSingleMethod) using
197 // this->NumberOfThreads threads.
199
200 // Description:
201 // Execute the MultipleMethods (as define by calling SetMultipleMethod
202 // for each of the required this->NumberOfThreads methods) using
203 // this->NumberOfThreads threads.
205
206 // Description:
207 // Set the SingleMethod to f() and the UserData field of the
208 // ThreadInfo that is passed to it will be data.
209 // This method (and all the methods passed to SetMultipleMethod)
210 // must be of type ThreadFunctionType and must take a single argument of
211 // type void *.
212 void SetSingleMethod(ThreadFunctionType, void *data );
213
214 // Description:
215 // Set the MultipleMethod at the given index to f() and the UserData
216 // field of the ThreadInfo that is passed to it will be data.
217 void SetMultipleMethod( int index, ThreadFunctionType, void *data );
218
219 // Description:
220 // Create a new thread for the given function. Return a thread id
221 // which is a number between 0 and IGTL_MAX_THREADS - 1. This id should
222 // be used to kill the thread at a later time.
223 int SpawnThread( ThreadFunctionType, void *data );
224
225 // Description:
226 // Terminate the thread that was created with a SpawnThreadExecute()
227 void TerminateThread( int thread_id );
228
229 // Description:
230 // Get the thread identifier of the calling thread.
232
233 // Description:
234 // Check whether two thread identifiers refer to the same thread.
237
238protected:
241
242 void PrintSelf(std::ostream& os) const;
243
244 // The number of threads to use
246
247 // An array of thread info containing a thread id
248 // (0, 1, 2, .. IGTL_MAX_THREADS-1), the thread count, and a pointer
249 // to void so that user data can be passed to each thread
250 ThreadInfo m_ThreadInfoArray[IGTL_MAX_THREADS];
251
252 // The methods
255
256 // Storage of MutexFunctions and ints used to control spawned
257 // threads and the spawned thread ids
258 int m_SpawnedThreadActiveFlag[IGTL_MAX_THREADS];
259 MutexLock::Pointer m_SpawnedThreadActiveFlagLock[IGTL_MAX_THREADS];
260 ThreadProcessIDType m_SpawnedThreadProcessID[IGTL_MAX_THREADS];
261 ThreadInfo m_SpawnedThreadInfoArray[IGTL_MAX_THREADS];
262
263//ETX
264
265 // Internal storage of the data
267 void *m_MultipleData[IGTL_MAX_THREADS];
268
269private:
270 MultiThreader(const MultiThreader&); // Not implemented.
271 void operator=(const MultiThreader&); // Not implemented.
272};
273
274} // namespace igtl
275#endif
276
277
278
279
280
void TerminateThread(int thread_id)
SmartPointer< Self > Pointer
igtlSetClampMacro(NumberOfThreads, int, 1, IGTL_MAX_THREADS)
int SpawnThread(ThreadFunctionType, void *data)
static int GetGlobalMaximumNumberOfThreads()
void SetMultipleMethod(int index, ThreadFunctionType, void *data)
static int GetGlobalDefaultNumberOfThreads()
static MultiThreaderIDType GetCurrentThreadID()
void MultipleMethodExecute()
static void SetGlobalMaximumNumberOfThreads(int val)
void SetSingleMethod(ThreadFunctionType, void *data)
virtual int GetNumberOfThreads()
ThreadFunctionType m_SingleMethod
static int ThreadsEqual(MultiThreaderIDType t1, MultiThreaderIDType t2)
igtlTypeMacro(MultiThreader, Object)
static void SetGlobalDefaultNumberOfThreads(int val)
SmartPointer< const Self > ConstPointer
void PrintSelf(std::ostream &os) const
Base class for most igtl classes.
Definition igtlObject.h:61
Implements transparent reference counting.
#define IGTL_MAX_THREADS
#define IGTLCommon_EXPORT
The "igtl" namespace contains all OpenIGTLink classes. There are several nested namespaces within the...
int MultiThreaderIDType
int ThreadProcessIDType
void(* ThreadFunctionType)(void *)

Generated for OpenIGTLink by Doxygen 1.9.8 written by Dimitri van Heesch, © 1997-2012