nrEngine::IThread Class Reference
[Kernel (Heart) of the nrEngine]

Abstract class to run a ITask in a thread controlled by Kernel. More...

Inheritance diagram for nrEngine::IThread:

nrEngine::ITask nrEngine::Clock nrEngine::EmptyTask nrEngine::EventManager nrEngine::IScript nrEngine::EmptyScript nrEngine::Script

Public Member Functions

virtual ~IThread ()

Protected Member Functions

 IThread ()
virtual void _noticeSuspend ()=0
virtual void _noticeResume ()=0
virtual void _noticeUpdate ()=0
virtual void _noticeStop ()=0

Friends

class Kernel
 Kernel is a friend class.

Detailed Description

Abstract class to run a ITask in a thread controlled by Kernel.

IThread is a base class for each kin dof tasks, allows also parallel running of a task as a thread. Kernel tasks are derived from thi class so a task has also a possibility to run in a own thread. Kernel controls the creation and managing of threads. Threads communicate through the engine's default communication protocol.

Writing a task as a thread for your application is not a big issue. You have to listen on kernel messages coming on the engine's default channel to be able to react on stop/suspend/resume messages. Kernel can not simply hold on a task. Because this can corrupt data if a task was performing any important calculations. Also this is not a good style. Kernel will provide a function to force a killing of a thread-task, but prevent using of this function.

Instead of killing the running threads, kernel will send a message about stopping the thread. A thread-task should react on this event to gracefully stop the execution.

The thread interface does provide neccessary functions to your tasks, so you do not have to think about managing the kernel messages by yourself. Instead of this the thread will call taskStop() method to indicate the task, that kernel want to stop the thread. Suspending the task will cause the kernel to let the according thread sleeping until the task is resumed. There is no other way how you can let the task sleep.

So how the thread interface cooperate with the kernel and the task interface. You write your task in the way, like if it were a simple task running sequentially in the kernel. Each ITask itnerface is a derived class from IThread but a thread portion of the code will only be active if you use the task as a thread for the kernel. By marking the task as a thread i.e. by adding the task to the kernel as a thread, you will activate the thread interface automatic. Kernel will start the thread and will call the taskInit() and taskStart() functions at the right moment. If the task is a thread, so it will call threadStart() instead which will call thread_taskStart() method of derived ITask interface. The method will call your taskStart() method, so you will not notice if there is a difference between starting a task as a task or as a thread.

In each cycle the kernel does call taskUpdate() method for sequential tasks. For parallel tasks (threads) it does not call anything, because thread interface already running and it will automaticaly call the appropritate taskUpdate() method, so the task is getting updated. Also the thread base class does check if there is any messages from the kernel (i.e. sleep or stop). If so it will call appropriate methods in ITask interface.

Our threads does yield their time slice to another threads after each execution cycle. In this manner we get a friendly and intuitive behaviour of threads running in parallel.

NOTE: The IThread interaface and Kernel does do all the job for you to manage themself as a threads and to let them run in parallel. The only one thing it can not manage for you is synchronisation. You have to worry about this by yourself by using of mutex locking mechanism.

Definition at line 82 of file IThread.h.


Constructor & Destructor Documentation

nrEngine::IThread::~IThread (  )  [virtual]

Virtual destructor so we are able to derive classes with overloaded methods.

Definition at line 32 of file IThread.cpp.

nrEngine::IThread::IThread (  )  [protected]

Protected constructor, so only the ITask-Interface and friends are able to create an instance of this class. In this way we protect wrong usage of this interface.

Definition at line 25 of file IThread.cpp.


Member Function Documentation

virtual void nrEngine::IThread::_noticeSuspend (  )  [protected, pure virtual]

Call this method if a thread goes into sleep mode. The derived task should reimplement this method to call the appropriate suspend method

virtual void nrEngine::IThread::_noticeResume (  )  [protected, pure virtual]

Call this method by waking up a thread. The derived task interface should call the appropritate resume method

virtual void nrEngine::IThread::_noticeUpdate (  )  [protected, pure virtual]

Thread does call this method in a main loop. The method should call the appropritate update method in ITask interface

virtual void nrEngine::IThread::_noticeStop (  )  [protected, pure virtual]

Call this method by stopping the execution of a thread. The underlying task has to remove all used data, because after calling of this method it will be destroyed


The documentation for this class was generated from the following files:
Generated on Wed Sep 12 23:19:43 2007 for nrEngine by  doxygen 1.5.1