#include <PoolExecutor.h>
Inheritance diagram for ZThread::PoolExecutor::
Public Methods | |
PoolExecutor (unsigned int min) throw () | |
PoolExecutor (unsigned int min, unsigned int max) throw () | |
virtual | ~PoolExecutor () throw () |
void | setMax (unsigned int max) throw (Synchronization_Exception) |
void | setMin (unsigned int) throw (Synchronization_Exception) |
unsigned int | getMax () throw (Synchronization_Exception) |
unsigned int | getMin () throw (Synchronization_Exception) |
virtual void | execute (Runnable *task) throw (Synchronization_Exception) |
virtual void | join () throw (Synchronization_Exception) |
virtual bool | available () throw (Synchronization_Exception) |
virtual unsigned int | threadCount () throw (Synchronization_Exception) |
Protected Types | |
typedef std::deque<Thread*> | WorkerList |
Typedef. | |
Protected Methods | |
virtual Thread* | createWorker () |
void | addWorker () throw (Synchronization_Exception) |
void | removeWorker () throw (Synchronization_Exception) |
Protected Attributes | |
WorkerList | _activeWorkers |
Active worker threads. | |
WorkerList | _unactiveWorkers |
Idle worker threads. | |
unsigned int | _maxThreads |
Minimum. | |
unsigned int | _minThreads |
Maximum. |
An executor allows the efficient execution of a number of small unrelated 'Runnable' tasks, or LWP's without the over head of a large number of synchronization controls needed in a thread per task solution.
The executor itself contains heavy weight synchronization controls to do this, but it will not need new ones for each additional task. This overcomes resource problems thread-per-task designs must face
In general, executors queue tasks for execution by another thread or by a pool of threads. The queuing of task allows for a good deal of control over the order of execution.
STATIC/NON-STATIC EXECUTORS:
All static executors should be marked as Daemon's using the template parameter. If the executor is non-static then it should not be marked as a daemon.
Static executors will use the new daemon thread feature, and so static executors do not need to be join()ed - however, they still need to be cancel()ed. This allows static executors to complete thier tasks after main exits, and solves static deadlock & deconstruction problems.
// Proper use of a static PoolExecutor static Executor *executor = new PoolExecutor<true>(THREAD_COUNT); int main() { // Add tasks executor->cancel(); }
Static executors can not be cancled from within thier destructors. The system will not execute the destructors for static objects until the threads have all been joined. Worker threads will not exit until the executor has been cancled. This will lead to a deadlock situation.
// Proper use of a non-static PoolExecutor int main() { Executor *executor = new PoolExecutor<false>(THREAD_COUNT); // Add tasks executor->cancel(); executor->join(); }
A template parameter was chosen because it allows only the code needed to implement that type of executor to be used, and eliminates the need for a daemon flag within the executor and a series of constant checks to take the approriate action.
The template also better reflects the nature of the choice of executor type. A static executor will not change and suddenly not act static, and vice-versa.
|
Create a new Executor that uses a pool of 'n' threads to execute submitted tasks.
|
|
Create a new Executor that uses a pool of threads to execute submitted tasks.
|
|
Destroy this Executor |
|
Internally used to manipulate the worker thread pool
|
|
Block until the executor becomes ready Reimplemented from ZThread::AbstractExecutor. |
|
Create a new worker for the Executor, allows extedned classes to more easily special Worker objects w/o reimplementing the entire executor. Reimplemented from ZThread::AbstractExecutor. |
|
Submit a new task
Reimplemented from ZThread::Executor. |
|
Get the current maximum number of threads
|
|
Get the current minimum number of threads
|
|
Wait for this Executor to complete. The calling thread is blocked until the last task has been completed by this executor or until the calling thread has been interrupted.
Reimplemented from ZThread::Executor. |
|
Internally used to manipulate the worker thread pool
|
|
Change the maximum number of threads in the pool. As tasks are added to the Executor, more threads will be added as needed - up to the maximum number of worker threads set.
|
|
Change the minimum number of threads in the pool. When currently running tasks complete, and the Thread executing the task completes it can be removed. A Thread will never be instantly killed because the minimum number of threads has changed. It is a gradual decline in the number of worker threads.
|
|
Count of active threads operation on behalf of this executor This does not guarantee that another thread won't have submitted a task that changes the number of executing threads before some action is taken in response to this. However, it is useful as a general heuristic
Reimplemented from ZThread::Executor. |