1
///////////////////////////////////////////////////////////////////////////
3
// Copyright (c) 2005, Industrial Light & Magic, a division of Lucas
6
// All rights reserved.
8
// Redistribution and use in source and binary forms, with or without
9
// modification, are permitted provided that the following conditions are
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
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.
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.
33
///////////////////////////////////////////////////////////////////////////
35
#ifndef INCLUDED_ILM_THREAD_POOL_H
36
#define INCLUDED_ILM_THREAD_POOL_H
38
//-----------------------------------------------------------------------------
40
// class Task, class ThreadPool, class TaskGroup
42
// Class ThreadPool manages a set of worker threads and accepts
43
// tasks for processing. Tasks added to the thread pool are
44
// executed concurrently by the worker threads.
46
// Class Thread provides an abstract interface for a task which
47
// a ThreadPool works on. Derived classes need to implement the
48
// execute() function which performs the actual task.
50
// Class TaskTroup allows synchronization on the completion of a set
51
// of tasks. Every task that is added to a ThreadPool belongs to a
52
// single TaskGroup. The destructor of the TaskGroup waits for all
53
// tasks in the group to finish.
55
// Note: if you plan to use the ThreadPool interface in your own
56
// applications note that the implementation of the ThreadPool calls
57
// operator delete on tasks as they complete. If you define a custom
58
// operator new for your tasks, for instance to use a custom heap,
59
// then you must also write an appropriate operator delete.
61
//-----------------------------------------------------------------------------
73
//-------------------------------------------------------
74
// Constructor -- creates numThreads worker threads which
75
// wait until a task is available.
76
//-------------------------------------------------------
78
ThreadPool (unsigned numThreads = 0);
81
//-----------------------------------------------------------
82
// Destructor -- waits for all tasks to complete, joins all
83
// the threads to the calling thread, and then destroys them.
84
//-----------------------------------------------------------
86
virtual ~ThreadPool ();
89
//--------------------------------------------------------
90
// Query and set the number of worker threads in the pool.
92
// Warning: never call setNumThreads from within a worker
93
// thread as this will almost certainly cause a deadlock
95
//--------------------------------------------------------
97
int numThreads () const;
98
void setNumThreads (int count);
101
//------------------------------------------------------------
102
// Add a task for processing. The ThreadPool can handle any
103
// number of tasks regardless of the number of worker threads.
104
// The tasks are first added onto a queue, and are executed
105
// by threads as they become available, in FIFO order.
106
//------------------------------------------------------------
108
void addTask (Task* task);
111
//-------------------------------------------
112
// Access functions for the global threadpool
113
//-------------------------------------------
115
static ThreadPool& globalThreadPool ();
116
static void addGlobalTask (Task* task);
133
virtual void execute () = 0;
154
} // namespace IlmThread