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 ...
随机推荐
- vue中事件修饰符详解(stop, prevent, self, once, capture, passive)
==.stop== 是阻止冒泡行为,不让当前元素的事件继续往外触发,如阻止点击div内部事件,触发div事件 ==.prevent== 是阻止事件本身行为,如阻止超链接的点击跳转,form表单的点击提 ...
- ActiveMQ JMX使用
一.说明 ActiveMQ使用过程中,可以使用自带的控制台进行相关的操作以及查看,但是当队列数相当多的时候,在查询以及整体的监控上,就可能相当的不便.所以可通过JMX的方式,进行MQ中队列相关指标的以 ...
- 在linux服务器上装svn版本管理,自动部署代码到web项目
在linux服务器上装svn版本管理,自动部署代码到项目 1.安装svn服务器端 yum install subversion 从镜像下载安装svn服务器端 中间会提示是否ok,输入y,确认 ...
- css/js禁止点击元素
css禁止点击页面元素,只需一句代码即可解决: pointer-events: none; 如果用js来控制的话那就是: $('#test').click(function(){ return fal ...
- python实现感知机线性分类模型
前言 感知器是分类的线性分类模型,其中输入为实例的特征向量,输出为实例的类别,取+1或-1的值作为正类或负类.感知器对应于输入空间中对输入特征进行分类的超平面,属于判别模型. 通过梯度下降使误分类的损 ...
- hihttps教你在Wireshark中提取旁路https解密源码
大家好,我是hihttps,专注SSL web安全研究,今天本文就是教大家怎样从wireshark源码中,提取旁路https解密的源码,非常值得学习和商业应用. 一.旁路https解密条件 众所周知, ...
- html标签和css基础语法与浏览器兼容性等相关基础学习
<!-- table的使用 --> <h3>前端日常</h3> <form action="https://www.baidu.com"& ...
- Spring 梳理-接收请求的输入(原)
Spring MVC 允许一下方式将客户端的数据传送到控制器的处理方法中 查询参数(Query Parameter) 表单参数(Form Parameter) 路径变量(Path Variable ...
- 深入MYSQL随笔
(1)查询生命周期:从客户端到服务器,然后在服务器上进行解析,生成执行计划,执行,并返回给客户端.执行是整个生命周期中,最重要的阶段. (2)慢查询基础:优化数据访问,减少访问的数据行. (3)查询不 ...
- 快学Scala 第八课 (嵌套类)
嵌套类: class Human { class Student{ val age = 10 } } object ClassDemo { def main(args: Array[String]): ...