#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\n", "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);
}

libevent::事件::定时器的更多相关文章

  1. libevent::事件::定时器2

    #define evtimer_new(b, cb, arg) event_new((b), -1, 0, (cb), (arg)) #include <cstdio> #include ...

  2. Libevent 事件管理和添加事件

    /**   我们先来看一下事件的创建*/struct event * event_new(struct event_base *base, evutil_socket_t fd, short even ...

  3. Libevent 事件循环(2)---事件被加入激活队列

    由Libevent 事件循环(1) 在上文中我们提到了libevent 事件循环event_dispatch 的大致过程,以epoll为例,我们看一下事件被如何加入激活队列. //在epoll_dis ...

  4. libevent中定时器的使用方法

    #include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include <signal.h ...

  5. Libevent 事件循环(1)

    // 事件的dispatch int event_base_loop(struct event_base *base, int flags) {    //得到采用的事件模型 epoll/epoll/ ...

  6. 并发编程---死锁||递归锁---信号量---Event事件---定时器

    死锁 互斥锁:Lock(),互斥锁只能acquire一次 递归锁:  RLock(),可以连续acquire多次,每acquire一次计数器+1,只有计数为0时,才能被抢到acquire # 死锁 f ...

  7. libevent(一)定时器Demo

    开始研究libevent,使用的版本是2.0.22. 实现一个定时器:每2秒执行一次printf. #include <stdio.h> #include <stdlib.h> ...

  8. libevent::事件

    /***************************************************************** 函数功能: 创建事件集 ********************* ...

  9. libevent 定时器示例

    程序执行结果: 每隔2秒,触发一次定时器. (2)98行:evtimer_assign在event.h中定义如下: 再来看看event_assign函数: ev     要初始化的事件对象 base  ...

随机推荐

  1. dart 大文件读取

    dart 中不可避免会出现文件读取的情况, 甚至是很大的文件, 比如 200M 的文件 如果一次性读入内存,虽然也行得通, 但是如果在 flutter 中开启个 200M 大小的字节数组, 一不小心可 ...

  2. Linux 笔记 - 第十二章 Shell 脚本

    博客地址:http://www.moonxy.com 一.前言 常见的编程语言分为两类:一类是编译型语言,如:C.C++ 和 Java等,它们远行前要经过编译器的编译.另一类是解释型语言,不需要编译, ...

  3. Day4 总结

  4. LCX使用心得

    最近在搞内网渗透,碰到 端口转发&边界处理 的时候,我们就可以借助一些小工具了,这类工具有很多,这里主要说明lcx的用法. lcx是个很老的端口转发工具,而它的使用也很简单.不过想要把lcx玩 ...

  5. 给idea设置默认使用的JDK

    一,前言 在文章给idea设置默认使用的maven配置中我给我的idea设置了默认使用的maven,并且在setting.xml文件中,设置了本地的maven仓库,这样就不会使用maven默认在C盘的 ...

  6. JAVA设计模式---总述篇

    一.设计模式(Design Pattern): 1.设计模式的概念 是前辈们对代码开发经验的总结,是解决特定问题的一系列套路.它不是语法规定,而是一套用来提高代码可复用性.可维护性.可读性.稳健性以及 ...

  7. selenium-01-2环境搭建

    首先下载好Eclipse 和配置好Java 环境变量 步骤省略, 请百度     方法一 添加jar包 官方下载地址: http://www.seleniumhq.org/download/ 官方地址 ...

  8. Javascript的基础

    ECMAScript(语法.标准) BOM(浏览器) DOM(网页) ECMAScript是一个标准,它规定了语法.类型.语句.关键字.保留子.操作符.对象.(相当于法律) BOM(浏览器对象模型): ...

  9. DeleteFolder

    import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; /*** * @author ...

  10. 异步Promise及Async/Await最完整入门攻略

    一.为什么有Async/Await? 我们都知道已经有了Promise的解决方案了,为什么还要ES7提出新的Async/Await标准呢? 答案其实也显而易见:Promise虽然跳出了异步嵌套的怪圈, ...