#include <AsyncExecutor.h>
Inheritance diagram for ZThread::AsyncExecutor::
Public Methods | |
AsyncExecutor () throw (Synchronization_Exception) | |
virtual | ~AsyncExecutor () throw () |
virtual void | execute (Runnable *task) throw (Synchronization_Exception) |
virtual void | join () throw (Synchronization_Exception) |
virtual unsigned int | threadCount () throw (Synchronization_Exception) |
Protected Methods | |
virtual Thread* | createWorker () |
Protected Attributes | |
Thread* | _worker |
Worker thread. |
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 AsyncExecutor static Executor *executor = new AsyncExecutor<true>; 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 AsyncExecutor int main() { Executor *executor = new AsyncExecutor<false>; // 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 AsyncExecutor, the actual initialization of any underlying resources used in constructing this Executor is deferred until the first task is executed.
|
|
Destroy the AsyncExecutor.
|
|
Default worker implementation will consume tasks Reimplemented from ZThread::AbstractExecutor. |
|
Enqueue another task for execution.
Reimplemented from ZThread::Executor. |
|
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. |
|
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. |