1.相关说明
本文主要自己对于libev的ev_timer定时器的代码流程梳理,主要有ev_timer结构体定义变量的初始化,定时器变量的参数设置,定时器变量的使用
2.相关代码流程
下面是图片
3.相关实现代码
main.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> #include <ev.h> static void timer_cb(struct ev_loop *loop, ev_timer *w, int revents) {fprintf(stdout, "%fs timeout\n", w->repeat);// ev_break(loop, EVBREAK_ALL); } int main() {struct ev_loop *loop;ev_timer *timer_watcher;loop = ev_default_loop(EVFLAG_NOENV);timer_watcher = calloc(1, sizeof(*timer_watcher));assert(("can not alloc memory", loop && timer_watcher));ev_timer_init(timer_watcher, timer_cb, 0, 2);ev_timer_start(loop, timer_watcher);ev_run(loop, 0);ev_loop_destroy(loop);free(timer_watcher); }
makefile
target := main CC := gcc CFLAGS += -g -Wall -fPIC LDFLAGS += -lev INC = -I /usr/local/libev/include LIB = -L /usr/local/libev/lib src += \main.c obj := $(patsubst %.c, %.o, $(src))$(target):$(obj)$(CC) -o $@ $^ $(INC) $(LIB) $(LDFLAGS) %.o:%.c$(CC) -o $@ -c $< $(INC) $(LIB) $(LDFLAGS) .PHONY:cleanclean:@rm -rf $(target) *.o