zoukankan      html  css  js  c++  java
  • libevent::事件::定时器

    复制代码
    #include <cstdio>
    #include <errno.h>
    #include <sys/types.h>
    #include <event.h>
    #include <event2/event.h>
    #include <event2/event_struct.h>
    #include <event2/event-config.h>
    #include <event2/util.h>
    
    int lasttime;
    
    static void
    timeout_cb(int fd, short event, void *arg)
    {
        struct timeval tv;
        struct event *timeout = (struct event *)(arg);
        int newtime = time(NULL);
    
        printf("%s: called at %d: %d
    ", "timeout_cb", newtime, newtime - lasttime);
        lasttime = newtime;
    
        evutil_timerclear(&tv);
        tv.tv_sec = 2;
    
        // 重新添加定时事件(定时事件触发后默认自动删除)  
        event_add(timeout, &tv);
    }
    
    int main(int argc, char **argv)
    {
        struct event timeout;
        struct timeval tv;
    
        //初始化event环境
        event_init();
    
        //设置事件
        evtimer_set(&timeout, timeout_cb, &timeout);
    
        evutil_timerclear(&tv);
        tv.tv_sec = 2;    //2s
    
        //注册事件
        event_add(&timeout, &tv);
    
        lasttime = time(NULL);
    
        //等待,分发,处理事件
        event_dispatch();
    
        return (0);
    }
    复制代码
  • 相关阅读:
    [转] linux下查看文件编码及修改编码
    offset Dimensions 详解
    style属性
    Using NodeLists
    Element Children
    Node、Document关系的探究
    Document、HTMLDocument关系的探究
    BOM Summary P268-P269
    Intervals and Timeouts
    Window Position
  • 原文地址:https://www.cnblogs.com/osbreak/p/10279870.html
Copyright © 2011-2022 走看看