SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Parallel.h
Go to the documentation of this file.
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2009 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9 
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 
29 
30 
31 ///
32 ///@file Parallel.h
33 ///@brief Automatically instantiate several threads
34 ///
35 ///@author Steve Parker
36 /// Department of Computer Science
37 /// University of Utah
38 ///@date June 1997
39 ///
40 
41 #ifndef Core_Thread_Parallel_h
42 #define Core_Thread_Parallel_h
43 
46 
47 namespace SCIRun {
48 /**************************************
49 
50 @class
51  Parallel
52 
53  KEYWORDS
54  Thread
55 
56 @details
57  Helper class to make instantiating threads to perform a parallel
58  task easier.
59 
60 ****************************************/
61 template<class T> class Parallel : public ParallelBase {
62 public:
63  //////////
64  /// Create a parallel object, using the specified member
65  /// function instead of <i>parallel</i>. This will
66  /// typically be used like:
67  /// <b><pre>Thread::parallel(Parallel&lt;MyClass&gt;(this, &amp;MyClass::mymemberfn), nthreads);</pre></b>
68  Parallel(T* obj, void (T::*pmf)(int));
69 
70  //////////
71  /// Destroy the Parallel object - the threads will remain alive.
72  virtual ~Parallel();
73  T* obj_;
74  void (T::*pmf_)(int);
75 protected:
76  virtual void run(int proc);
77 private:
78  // Cannot copy them
79  Parallel(const Parallel&);
80  Parallel<T>& operator=(const Parallel<T>&);
81 };
82 
83 template<class T>
84 void
86 {
87  // Copy out do make sure that the call is atomic
88  T* obj=obj_;
89  void (T::*pmf)(int) = pmf_;
90  if(wait_)
91  wait_->up();
92  (obj->*pmf)(proc);
93  // Cannot do anything here, since the object may be deleted by the
94  // time we return
95 }
96 
97 template<class T>
98 Parallel<T>::Parallel(T* obj, void (T::*pmf)(int))
99  : obj_(obj), pmf_(pmf)
100 {
101  wait_=0; // This may be set by Thread::parallel
102 } // End namespace SCIRun
103 
104 template<class T>
106 {
107 }
108 
109 } //End namespace SCIRun
110 
111 #endif
112 
113 
114 
115 
void(T::* pmf_)(int)
Definition: Parallel.h:74
virtual void run(int proc)
The thread body
Definition: Parallel.h:85
Definition: ParallelBase.h:62
Helper class to instantiate several threads.
Definition: Parallel.h:61
virtual ~Parallel()
Destroy the Parallel object - the threads will remain alive.
Definition: Parallel.h:105
Parallel(T *obj, void(T::*pmf)(int))
Definition: Parallel.h:98
T * obj_
Definition: Parallel.h:73
Semaphore * wait_
Definition: ParallelBase.h:71