![]() |
| Home |
|
|
Starting Objects' Methods as TasksThe code of a task must always be a function with zero or one parameters. This condition cannot be met by methods of objects, because they always have the invisible parameter "this". If a method must be used as a task anyhow, two strategies are possible: Encapsulate the method by a normal function and start the function as a task:
#include <Rtk32.h>
class MyObject {
public:
virtual void Task(void);
};
void RTKAPI RTKernelTask(void * p)
{
MyObject Object1;
Object1.Task();
}
void main(void)
{
RTKernelInit(0);
RTKRTLCreateThread(RTKernelTask, 7, 4096, 0, NULL, "Object-Task");
}
Alternatively, the task can be sent a pointer to the object to use:
#include <Rtk32.h>
class MyObject {
public:
virtual void Task(void);
};
void RTKAPI RTKernelTask(void * Object)
{
((MyObject*) Object)->Task();
delete (MyObject*) Object;
}
void main(void)
{
RTKernelInit(2);
H = RTKRTLCreateThread(RTKernelTask, 7, 4096, 0, new MyObject, "Object-Task");
}
The second alternative has the advantage that any number of tasks can be started with a single task function; however, each task uses a different object. These tasks, in turn, can execute different code by using derived objects which redefine the method Task.
|