SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ThreadPool.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 ThreadPool
33 ///@brief A pool of threads
34 ///
35 ///@author Steve Parker
36 /// Department of Computer Science
37 /// University of Utah
38 ///@date January 2000
39 ///
40 
41 #ifndef Core_Thread_ThreadPool_h
42 #define Core_Thread_ThreadPool_h
43 
50 
51 #include <vector>
52 
53 
54 namespace SCIRun {
55  class ThreadGroup;
56 class ThreadPoolHelper;
57 /**************************************
58 
59 @class
60  ThreadPool
61 
62  KEYWORDS
63  ThreadPool
64 
65 @details
66  The ThreadPool class groups a bunch of worker threads.
67 
68 ****************************************/
69 class ThreadPool {
70 public:
71  //////////
72  /// Create a thread pool. <tt>name</tt> should be a static
73  /// string which describes the primitive for debugging purposes.
74  ThreadPool(const char* name);
75 
76  //////////
77  /// Destroy the pool and shutdown all threads
78  ~ThreadPool();
79 
80  //////////
81  /// Start up several threads that will run in parallel.
82  /// The caller will block until all of the threads return.
83  void parallel(const ParallelBase& helper, int nthreads);
84 
85  //////////
86  /// Start up several threads that will run in parallel.
87  /// The caller will block until all of the threads return.
88  template<class T>
89  void parallel(T* ptr, void (T::*pmf)(int), int numThreads) {
90  parallel(Parallel<T>(ptr, pmf),
91  numThreads);
92  }
93 
94  //////////
95  /// Another overloaded version of parallel that passes 1 argument
96  template<class T, class Arg1>
97  void parallel(T* ptr, void (T::*pmf)(int, Arg1),
98  int numThreads,
99  Arg1 a1) {
100  parallel(Parallel1<T, Arg1>(ptr, pmf, a1),
101  numThreads);
102  }
103 
104  //////////
105  /// Another overloaded version of parallel that passes 2 arguments
106  template<class T, class Arg1, class Arg2>
107  void parallel(T* ptr, void (T::* pmf)(int, Arg1, Arg2),
108  int numThreads,
109  Arg1 a1, Arg2 a2) {
110  parallel(Parallel2<T, Arg1, Arg2>(ptr, pmf, a1, a2),
111  numThreads);
112  }
113 
114  //////////
115  /// Another overloaded version of parallel that passes 3 arguments
116  template<class T, class Arg1, class Arg2, class Arg3>
117  void parallel(T* ptr, void (T::* pmf)(int, Arg1, Arg2, Arg3),
118  int numThreads,
119  Arg1 a1, Arg2 a2, Arg3 a3) {
120  parallel(Parallel3<T, Arg1, Arg2, Arg3>(ptr, pmf, a1, a2, a3),
121  numThreads);
122  }
123 
124 private:
125  const char* name_;
126  ThreadGroup* group_;
127  Mutex lock_;
128  std::vector<ThreadPoolHelper*> threads_;
129  int numThreads_;
130  Barrier barrier;
131  friend class ThreadPoolHelper;
132  void wait();
133 
134  // Cannot copy them
135  ThreadPool(const ThreadPool&);
136  ThreadPool& operator=(const ThreadPool&);
137 };
138 } // End namespace SCIRun
139 
140 #endif
141 
void parallel(T *ptr, void(T::*pmf)(int), int numThreads)
Definition: ThreadPool.h:89
Definition: ThreadPool.h:69
Definition: ParallelBase.h:62
Definition: Barrier.h:71
void parallel(T *ptr, void(T::*pmf)(int, Arg1), int numThreads, Arg1 a1)
Another overloaded version of parallel that passes 1 argument.
Definition: ThreadPool.h:97
Definition: Parallel3.h:61
Definition: Parallel1.h:61
void parallel(T *ptr, void(T::*pmf)(int, Arg1, Arg2, Arg3), int numThreads, Arg1 a1, Arg2 a2, Arg3 a3)
Another overloaded version of parallel that passes 3 arguments.
Definition: ThreadPool.h:117
Definition: Mutex.h:65
Automatically instantiate several threads, with 2 arguments.
ThreadPool(const char *name)
const char * name[]
Definition: BoostGraphExampleTests.cc:87
Definition: Parallel2.h:61
Definition: Parallel.h:61
void parallel(const ParallelBase &helper, int nthreads)
friend class ThreadPoolHelper
Definition: ThreadPool.h:131
Automatically instantiate several threads, with 1 argument.
void parallel(T *ptr, void(T::*pmf)(int, Arg1, Arg2), int numThreads, Arg1 a1, Arg2 a2)
Another overloaded version of parallel that passes 2 arguments.
Definition: ThreadPool.h:107
~ThreadPool()
Destroy the pool and shutdown all threads.
Definition: ThreadGroup.h:68