- Notifications
You must be signed in to change notification settings - Fork278
timer
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
parent directory.. | ||||
- Hashed timing wheel implementation.
- Provides fast cancel(O(1)) and poll operations compared to a priority queue.
- Timers in the same hash slot are not ordered between each other. So, basically
this data structure trades accuracy for performance. Schedule a timer for
10000ms and another for 10001ms and you might see 10001ms timer expires
just before 10000ms timer. Default tick is 16 ms, so, timers in the same 16ms
interval may expire out of order.
#include"sc_timer.h"#include<errno.h>#include<stdio.h>#include<time.h>uint64_ttime_ms();voidsleep_ms(uint64_tmilliseconds);voidcallback(void*arg,uint64_ttimeout,void*data){structsc_timer*timer=arg;char*timer_name=data;printf("timeout : %zu, data : %s \n",timeout,timer_name);// Schedule backsc_timer_add(timer,"timer1",1000);}intmain(intargc,char*argv[]){uint64_tnext_timeout;structsc_timertimer;sc_timer_init(&timer,time_ms());sc_timer_add(&timer,"timer1",1000);while (true) {next_timeout=sc_timer_timeout(&timer,time_ms(),&timer,callback);sleep_ms(next_timeout); }return0;}