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\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::事件::定时器的更多相关文章
- libevent::事件::定时器2
#define evtimer_new(b, cb, arg) event_new((b), -1, 0, (cb), (arg)) #include <cstdio> #include ...
- Libevent 事件管理和添加事件
/** 我们先来看一下事件的创建*/struct event * event_new(struct event_base *base, evutil_socket_t fd, short even ...
- Libevent 事件循环(2)---事件被加入激活队列
由Libevent 事件循环(1) 在上文中我们提到了libevent 事件循环event_dispatch 的大致过程,以epoll为例,我们看一下事件被如何加入激活队列. //在epoll_dis ...
- libevent中定时器的使用方法
#include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include <signal.h ...
- Libevent 事件循环(1)
// 事件的dispatch int event_base_loop(struct event_base *base, int flags) { //得到采用的事件模型 epoll/epoll/ ...
- 并发编程---死锁||递归锁---信号量---Event事件---定时器
死锁 互斥锁:Lock(),互斥锁只能acquire一次 递归锁: RLock(),可以连续acquire多次,每acquire一次计数器+1,只有计数为0时,才能被抢到acquire # 死锁 f ...
- libevent(一)定时器Demo
开始研究libevent,使用的版本是2.0.22. 实现一个定时器:每2秒执行一次printf. #include <stdio.h> #include <stdlib.h> ...
- libevent::事件
/***************************************************************** 函数功能: 创建事件集 ********************* ...
- libevent 定时器示例
程序执行结果: 每隔2秒,触发一次定时器. (2)98行:evtimer_assign在event.h中定义如下: 再来看看event_assign函数: ev 要初始化的事件对象 base ...
随机推荐
- px、em、rem、%、vw、vh、vm这些单位的区别
1.px px就是像素,也是我们现在经常使用的基本单位,比如常常听到的电脑像素是1024x768的,表示的是水平方向是1024个像素点,垂直方向是768个像素点. 2.em em参考物是父元素的fon ...
- 004:CSS三大重点之二:浮动(拖标、不占位置、转行内块)
目录 1:拖标.不占位.转行内块 2:首先浮动的盒子需要和标准流的父级搭配使用,其次 特别的注意浮动可以使元素显示模式体现为行内块特性. 3:清除浮动 前言 CSS的定位机制有3种:普通流(标准流). ...
- 基于Asp.Net Core MVC和AdminLTE的响应式管理后台之侧边栏处理
说明: .NET Core版本为:2.2 AdminLTE版本为:2.4.18 Bootstrap版本为:3.4.1 font-awesome版本为:4.7.0 1.新建项目:AdminLteDemo ...
- Spring Cloud Gateway 之请求坑位[微服务IP不同请求会失败]
问题产生背景 在使用Spring Cloud Gateway过程中,希望配置多Routes映射不同的微服务,因为Gateway 和Zuul的访问路径不同(zuul 会带有服务service Id),造 ...
- web前端开发面试题(附答案)-1
1.浏览器中输入url到网页显示,整个过程发生了什么 域名解析 发起tcp三次握手 建立tcp连接之后发起htttp请求 服务器端响应http请求,浏览器得到html代码 浏览器器解析html代码,并 ...
- redis分布式锁-自动超时锁(在用)
1.加锁代码结构 2.解锁代码结构 3.java实例 4.测试类 5.测试日志 加锁代码结构 def acquire_lock_with_timeout(conn,lockname,acquire_t ...
- windows如何利用计划任务自动关机?
第一步打开控制面板,然后选择计划任务,打开它 选择创建基本任务 输入任务名称,描述,选择下一步 根据需要选择,我这里选择的是每天,然后选择下一步 选择任务开始时间,然后选择下一步 选择启动程序,然后选 ...
- shell管道与重定向
输出重定向 $ ls -l > lsoutput 这条命令将ls命令执行后的结果输入出到lsoutput文件中. 在linux shell中使用符号 > ,< 来完成输入输出的重定向 ...
- 阿里云服务器CentOS6.9安装SVN
1.安装SVN yum -y install subversion 出现Complete表明安装成功 2.创建SVN仓库目录 mkdir -p /data/svn/repositories/yyksv ...
- Android开发——Kotlin开发APP使用笔记
之前一直使用java来开发Android项目,学了新的kotlin语言,前来试一试,并说一下kotlin对Android的一些功能增强 创建项目 我使用的是Android Studio3.0+,所以默 ...