码迷,mamicode.com
首页 > 其他好文 > 详细

libevent timer定时器

时间:2018-11-27 13:43:57      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:pat   回调   ams   color   timer   stream   eva   div   patch   

每隔一秒循环执行回调函数
#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定时器

标签:pat   回调   ams   color   timer   stream   eva   div   patch   

原文地址:https://www.cnblogs.com/lsaejn/p/10025765.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!