SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Parallel2.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 Parallel2.h
33 ///@brief Automatically instantiate several threads, with 2 arguments
34 ///
35 ///@author Steve Parker
36 /// Department of Computer Science
37 /// University of Utah
38 ///@date June 2000
39 ///
40 
41 #ifndef Core_Thread_Parallel2_h
42 #define Core_Thread_Parallel2_h
43 
46 
47 namespace SCIRun {
48 /**************************************
49 
50 @class
51  Parallel2
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 Arg1, class Arg2> class Parallel2 : public ParallelBase {
62 public:
63  //////////
64  /// Create a Parallel2 object, using the specified member
65  /// function instead of <i>Parallel2</i>. This will
66  /// typically be used like:
67  /// <b><pre>Thread::Parallel2(Parallel2&lt;MyClass&gt;(this, &amp;MyClass::mymemberfn), nthreads);</pre></b>
68  Parallel2(T* obj, void (T::*pmf)(int, Arg1, Arg2), Arg1 a1, Arg2 a2);
69 
70  //////////
71  /// Destroy the Parallel2 object - the threads will remain alive.
72  virtual ~Parallel2();
73  T* obj_;
74  void (T::*pmf_)(int, Arg1, Arg2);
75  Arg1 a1;
76  Arg2 a2;
77 protected:
78  virtual void run(int proc);
79 private:
80  /// Cannot copy them
81  Parallel2(const Parallel2&);
83 };
84 
85 template<class T, class Arg1, class Arg2>
86 void
88 {
89  // Copy out do make sure that the call is atomic
90  T* obj=obj_;
91  void (T::*pmf)(int, Arg1, Arg2) = pmf_;
92  if(wait_)
93  wait_->up();
94  (obj->*pmf)(proc, a1, a2);
95  // Cannot do anything here, since the object may be deleted by the
96  // time we return
97 }
98 
99 template<class T, class Arg1, class Arg2>
101  void (T::*pmf)(int, Arg1, Arg2),
102  Arg1 a1, Arg2 a2)
103  : obj_(obj), pmf_(pmf), a1(a1), a2(a2)
104 {
105  wait_=0; // This may be set by Thread::parallel
106 } // End namespace SCIRun
107 
108 template<class T, class Arg1, class Arg2>
110 {
111 }
112 
113 } //End namespace SCIRun
114 #endif
115 
116 
117 
118 
119 
virtual ~Parallel2()
Destroy the Parallel2 object - the threads will remain alive.
Definition: Parallel2.h:109
Definition: ParallelBase.h:62
Helper class to instantiate several threads.
virtual void run(int proc)
The thread body
Definition: Parallel2.h:87
Definition: Parallel2.h:61
Arg2 a2
Definition: Parallel2.h:76
Arg1 a1
Definition: Parallel2.h:75
Parallel2(T *obj, void(T::*pmf)(int, Arg1, Arg2), Arg1 a1, Arg2 a2)
Definition: Parallel2.h:100
T * obj_
Definition: Parallel2.h:73
Semaphore * wait_
Definition: ParallelBase.h:71
void(T::* pmf_)(int, Arg1, Arg2)
Definition: Parallel2.h:74