#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. c语言运算优先级与结合方向的问题

    c语言运算的结合方向一直困扰我,不过我终于get到了哈哈 先附上c语言运算优先级和结合方向图片 以上有c语言一共的15个优先级,从上往下递增 c语言的运算符号有很多,当我们打代码或者看代码的时候符号多 ...

  2. 07 (OC)* XIB原理和Xib、storyBoard、代码的优缺点

    1:可读性 2:可视化界面.立马看到 3:开发速度. 4:复用性 5:维护性差 本质 编译时对xml文件做了如下操作1,读取xml文件,生成所有界面对象,生成所有object(即自定义的control ...

  3. 网站启动,报编译错误:类型“ASP.global_asax”同时存在两个文件夹的问题

    CS0433: The type 'ASP.global_asax' exists in both 'c:\Windows\Microsoft.NET\Framework64\v4.0.30319\T ...

  4. SqlServer 统计1-12月份 每个月的数据(临时表)

    想做一个年度图表 效果如下,通过sqlserver来统计今年1-12月份每个月的数据,效果如下 sql语句网上找了很多都没有找到满意的 找到的其中一种写法是这样子的 这种写法对于前端来说很方便 不用进 ...

  5. asp.net core IdentityServer4 实现 implicit(隐式许可)实现第三方登录

    前言 OAuth 2.0默认四种授权模式(GrantType) 授权码模式(authorization_code) 简化模式(implicit) 密码模式(resource owner passwor ...

  6. HTML5-常用正则表达式

    有关H5正则表达式的一些常用式子,希望热爱编程的同学们多多指教,还有也希望可以关注收藏本站哦!❤^_^❤ 一.校验数字的表达式 1. 数字:^[0-9]*$ 2. n位的数字:^\d{n}$ 3. 至 ...

  7. 对于java的Sting.intern()的一些注意

    今天翻看书时遇到了这样一个问题,对于String.intern()方法又有了一些认识和看法.首先我们看它的api 大意就是intern()方法会在常量池中记录首次出现的实例引用,但是在jdk1.6中却 ...

  8. Fresco添加HTTP请求头

    项目中用Fresco来管理图片由于服务器图片有不同的版本需要根据客户端的屏幕密度来选择不同的图片共享一份用OkHttp下载图片并添加HTTP头代码. public class OkHttpNetwor ...

  9. 孙悟空的七十二变是那般?--java类型的七十二变揭秘

    故事背景 在<西游记>原著第六回,孙悟空大闹天宫反下界,玉帝派十万天兵围剿,却被打得落花流水.玉帝不得不放下架子,请自己外甥二郎神回来支援.孙悟空与二郎神本事差不多,两人斗得不分胜负,但二 ...

  10. Shell之三剑客

    目录 Shell之三剑客 参考 Grep Sed Awk Shell之三剑客