1. 程式人生 > >libevent中定時器的使用方法

libevent中定時器的使用方法

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow

也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!

                 
    #include <stdio.h>    #include <stdlib.h>    #include
<sys/time.h>
    #include <signal.h>    #include <event2/event.h>    #include <event2/event_struct.h>    struct self_tv{    struct event* timeout;    struct timeval tv;    int
order;    };    void sigroute(int fd, short event, void* arg) {    struct self_tv* st = (struct self_tv*)arg;    printf(“%d wake up\n”, st->order);    st->tv.tv_sec = st->tv.tv_sec + 1;    evtimer_add(st->timeout, &(st->tv));    }    int
main()   
{    struct event_base* eb;    struct self_tv* st;    int i=1;    eb = event_base_new();    for(i=0;i<=1000;i++) {    st = (struct self_tv*)malloc(sizeof(struct self_tv));    st->tv.tv_sec = 5;    st->tv.tv_usec = 0;    st->order = i;    st->timeout = evtimer_new(eb, sigroute, st);    evtimer_add(st->timeout, &st->tv);    }    event_base_dispatch(eb);    return 0;    }         libevent的timer定時器在沒有指定EV_PERSIST時是一次性事件。。。在回撥裡都需要再次evtimer_add一次。

           

給我老師的人工智慧教程打call!http://blog.csdn.net/jiangjunshow

這裡寫圖片描述