一个最小堆简单定时器的实现demo

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>

#define HEAP_SIZE   100
typedef struct Heap_t Heap_t;
typedef struct Timer_t Timer_t;
typedef struct Timer_Ctl_t Timer_Ctl_t;
typedef void (*cb_func)(void *, Timer_Ctl_t*, void *);

struct Heap_t{
  int size;
  int buffer[HEAP_SIZE];
};

struct Timer_t{
  int expire;
  cb_func cb;
};

struct Timer_Ctl_t{
  Heap_t heap;
  Timer_t timer;
};

/*初始化*/
void Timer_Init(Timer_Ctl_t *timer_ctl)
{
  timer_ctl->heap.size = 0;
  timer_ctl->timer.expire = 0;
}

/*插入数据到堆中*/
int Insert_Heap(Heap_t *heap, int value)
{
  int size = heap->size;
  if(size == HEAP_SIZE)
  {
    printf("This Heap is full\n");
    return 1;
  }
  heap->size ++;
  heap->buffer[size] = value;
  if(size > 0)
  {
    int i = size;
    int j = (i-1)/2;
    while(j >= 0 && i > 0)
    {
        if( heap->buffer[j] <= value)
            break;
        else
        {
            heap->buffer[i] = heap->buffer[j];
            i = j;
            j = (i-1)/2;
        }
    }
    heap->buffer[i]=value;
  }
  return 0;
}

/*删除堆顶数据*/
int Delete_Heap(Heap_t *heap)
{
  int parent,child = 1;
  if(heap->size == 0)
  {
    printf("This Heap is Empty\n");
    return 1;
  }
  heap->size --;
  int size = heap->size;
  for(parent = 0; parent*2+1 < size ; parent = child)
  {
    child = 2 * parent + 1;
    if(child + 1 < size)
    {
      if(heap->buffer[child] > heap->buffer[child+1])
        child ++;
    }
    if(heap->buffer[size] < heap->buffer[child])
      break;
    else
      heap->buffer[parent] =  heap->buffer[child];
  }
  heap->buffer[parent] = heap->buffer[size];
  return 0;
}

/*获取当前时间*/
time_t Get_Current_Time()
{
  return time(NULL);
}

/*获取定时时间*/
time_t Parse_Expire_Time(int delay)
{
  return time(NULL)+delay;
}

/*启动定时器*/
void Ev_Timer_Start(int Timeout_s,cb_func cb,Timer_Ctl_t *timer_ctl)
{
  int expire = Parse_Expire_Time(Timeout_s);
  timer_ctl->timer.cb = cb;
  Insert_Heap(&timer_ctl->heap,expire);
}

/*定时器回调函数*/
void Test_CallBack_Func(void *arg1, Timer_Ctl_t *timer_ctl , void *arg2)
{
  static int timeout = 0;
  printf("Hello World\n");
  timeout ++;
  Ev_Timer_Start(timeout,Test_CallBack_Func,timer_ctl);
}

/*定时器运行*/
void Timer_Run(Timer_Ctl_t *timer_ctl)
{
  int size = timer_ctl->heap.size;
  if(size == 0)
    return ;
  time_t now = Get_Current_Time();
  timer_ctl->timer.expire = timer_ctl->heap.buffer[0];
  if(timer_ctl->timer.expire <= now)
  {
    printf("now:%d\n",(int)now);
    if(timer_ctl->timer.cb)
      timer_ctl->timer.cb(NULL,timer_ctl,NULL);
    Delete_Heap(&timer_ctl->heap);
  }
  usleep(20000);
}

int main()
{
  Timer_Ctl_t timer_ctl;
  Timer_Init(&timer_ctl);
  Ev_Timer_Start(2,Test_CallBack_Func,&timer_ctl);
  while(1)
  {
    Timer_Run(&timer_ctl);
  }
  return 0;
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值