libevent timer定时器

Posted lsaejn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了libevent timer定时器相关的知识,希望对你有一定的参考价值。

每隔一秒循环执行回调函数
#include <iostream>
#include <event2/event.h>

struct cb_arg
{
    struct event *ev;
    struct timeval tv;
};

void timeout_cb(int fd, short event, void *params)
{
    puts("111");
    struct cb_arg *arg = (struct cb_arg*)params;
    struct event *ev = arg->ev;
    struct timeval tv = arg->tv;

    evtimer_add(ev, &tv);
}

int main()
{
    struct event_base *base = event_base_new();
    struct event *timeout = NULL;
    struct timeval tv = {1, 0};
    struct cb_arg arg;

    timeout = evtimer_new(base, timeout_cb, &arg);
    arg.ev = timeout;
    arg.tv = tv;
    evtimer_add(timeout, &tv);
    event_base_dispatch(base);
    evtimer_del(timeout);

    return 0;
}

 

以上是关于libevent timer定时器的主要内容,如果未能解决你的问题,请参考以下文章

测试片段不执行定时器或示例超时

libevent总结(上)

libevent定时器是怎么实现的

libevent总结学习

libevent:使计时器持久化

浅析libevent