return (EXIT_SUCCESS); }运行结果为:task : [Task1] is ready to run [a = 1.200000, b = 2.300000] [(a + b) * (a - b) = -3.850000] cancel is invoked here task : [Task2] is ready to run another type of execute,just print out some information cancel is invoked here好了,下面详细看看实现:定义接口首先,定义Task和Executor两个实体的接口:Task接口,注意其中的_this字段,这个指针在后边有很重要的作用,用于hold整个Task的实例。然后是一个taskName的字符串,和一个函数指针,这个指针在初始化(构造)Task时传入。这个execute()函数比较有意思,它不在内部使用,而是让执行器回调执行的。#ifndef _ITASK_H #define _ITASK_H
/* * The construction of Task object. * name : the name of the task * execute : execute method of the task * */ Task *TaskConstruction(char *name, void (*execute)()){ task = (Task*)malloc(sizeof(strlen(name))+sizeof(execute)); task->taskName = name; task->execute = execute; task->_this = task;