#include<stdio.h>
#include <unistd.h>
#define uchar unsigned char
#define TASKNUM 2
typedef struct TimerInfo{
uchar id;
uchar time_pice;
uchar left_time;
uchar all_time;
void (*Thing)();
}TIMERINFO;
void Thing1();
void Thing2();
TIMERINFO TF[TASKNUM]={};
void ResigerTask(uchar id,uchar time_pice,uchar left_time,uchar all_time,void( *thing)()){
TF[id].id=id;
TF[id].time_pice=time_pice;
TF[id].left_time=left_time;
TF[id].all_time=all_time;
TF[id].Thing=thing;
}
void InitTask(){
ResigerTask(0,10,100,100,Thing1);
ResigerTask(1,50,100,100,Thing2);
}
void Thing1(){
printf("Thing1\n");
}
void Thing2(){
printf("Thing2\n");
}
void CheckTimeEven(){
for(uchar i=0;i<TASKNUM;i++){
//if((TF[i].Thing)==NULL){
// printf("Now we only have %d things,all %d\n",i,TASKNUM);
// break;
//}
TF[i].left_time-=TF[i].time_pice;
printf("CheckTimeEven:id:%d left_time:%d\n",TF[i].id,TF[i].left_time);
if(TF[i].left_time==0){
TF[i].left_time=TF[i].all_time;
TF[i].Thing();
}
}
}
int main(){
InitTask();
while(1){
CheckTimeEven();
printf("=========================================\n");
sleep(1);
}
return 0;
#include <unistd.h>
#define uchar unsigned char
#define TASKNUM 2
typedef struct TimerInfo{
uchar id;
uchar time_pice;
uchar left_time;
uchar all_time;
void (*Thing)();
}TIMERINFO;
void Thing1();
void Thing2();
TIMERINFO TF[TASKNUM]={};
void ResigerTask(uchar id,uchar time_pice,uchar left_time,uchar all_time,void( *thing)()){
TF[id].id=id;
TF[id].time_pice=time_pice;
TF[id].left_time=left_time;
TF[id].all_time=all_time;
TF[id].Thing=thing;
}
void InitTask(){
ResigerTask(0,10,100,100,Thing1);
ResigerTask(1,50,100,100,Thing2);
}
void Thing1(){
printf("Thing1\n");
}
void Thing2(){
printf("Thing2\n");
}
void CheckTimeEven(){
for(uchar i=0;i<TASKNUM;i++){
//if((TF[i].Thing)==NULL){
// printf("Now we only have %d things,all %d\n",i,TASKNUM);
// break;
//}
TF[i].left_time-=TF[i].time_pice;
printf("CheckTimeEven:id:%d left_time:%d\n",TF[i].id,TF[i].left_time);
if(TF[i].left_time==0){
TF[i].left_time=TF[i].all_time;
TF[i].Thing();
}
}
}
int main(){
InitTask();
while(1){
CheckTimeEven();
printf("=========================================\n");
sleep(1);
}
return 0;
}
实现:实现了两个事件,不同执行时间执行对应事件~~