HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
IlmThreadPool.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2005-2012, Industrial Light & Magic, a division of Lucas
4 // Digital Ltd. LLC
5 //
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are
10 // met:
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 // * Neither the name of Industrial Light & Magic nor the names of
18 // its contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 ///////////////////////////////////////////////////////////////////////////
34 
35 #ifndef INCLUDED_ILM_THREAD_POOL_H
36 #define INCLUDED_ILM_THREAD_POOL_H
37 
38 
39 //-----------------------------------------------------------------------------
40 //
41 // class Task, class ThreadPool, class TaskGroup
42 //
43 // Class ThreadPool manages a set of worker threads and accepts
44 // tasks for processing. Tasks added to the thread pool are
45 // executed concurrently by the worker threads.
46 //
47 // Class Task provides an abstract interface for a task which
48 // a ThreadPool works on. Derived classes need to implement the
49 // execute() function which performs the actual task.
50 //
51 // Class TaskGroup allows synchronization on the completion of a set
52 // of tasks. Every task that is added to a ThreadPool belongs to a
53 // single TaskGroup. The destructor of the TaskGroup waits for all
54 // tasks in the group to finish.
55 //
56 // Note: if you plan to use the ThreadPool interface in your own
57 // applications note that the implementation of the ThreadPool calls
58 // operator delete on tasks as they complete. If you define a custom
59 // operator new for your tasks, for instance to use a custom heap,
60 // then you must also write an appropriate operator delete.
61 //
62 //-----------------------------------------------------------------------------
63 
64 #include "IlmThreadNamespace.h"
65 #include "IlmThreadExport.h"
66 
68 
69 class TaskGroup;
70 class Task;
71 
72 
74 {
75  public:
76 
77  //-------------------------------------------------------
78  // Constructor -- creates numThreads worker threads which
79  // wait until a task is available.
80  //-------------------------------------------------------
81 
82  ThreadPool (unsigned numThreads = 0);
83 
84 
85  //-----------------------------------------------------------
86  // Destructor -- waits for all tasks to complete, joins all
87  // the threads to the calling thread, and then destroys them.
88  //-----------------------------------------------------------
89 
90  virtual ~ThreadPool ();
91 
92 
93  //--------------------------------------------------------
94  // Query and set the number of worker threads in the pool.
95  //
96  // Warning: never call setNumThreads from within a worker
97  // thread as this will almost certainly cause a deadlock
98  // or crash.
99  //--------------------------------------------------------
100 
101  int numThreads () const;
102  void setNumThreads (int count);
103 
104 
105  //------------------------------------------------------------
106  // Add a task for processing. The ThreadPool can handle any
107  // number of tasks regardless of the number of worker threads.
108  // The tasks are first added onto a queue, and are executed
109  // by threads as they become available, in FIFO order.
110  //------------------------------------------------------------
111 
112  void addTask (Task* task);
113 
114 
115  //-------------------------------------------
116  // Access functions for the global threadpool
117  //-------------------------------------------
118 
119  static ThreadPool& globalThreadPool ();
120  static void addGlobalTask (Task* task);
121 
122  struct Data;
123 
124  protected:
125 
126  Data * _data;
127 };
128 
129 
131 {
132  public:
133 
134  Task (TaskGroup* g);
135  virtual ~Task ();
136 
137  virtual void execute () = 0;
138  TaskGroup * group();
139 
140  protected:
141 
143 };
144 
145 
147 {
148  public:
149 
150  TaskGroup();
151  ~TaskGroup();
152 
153  struct Data;
154  Data* const _data;
155 };
156 
157 
159 
160 #endif // INCLUDED_ILM_THREAD_POOL_H
Data * _data
GLboolean GLboolean g
Definition: glcorearb.h:1221
#define ILMTHREAD_INTERNAL_NAMESPACE_HEADER_ENTER
#define ILMTHREAD_INTERNAL_NAMESPACE_HEADER_EXIT
#define ILMTHREAD_EXPORT
GLint GLsizei count
Definition: glcorearb.h:404
TaskGroup * _group
Data *const _data