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 ...
随机推荐
- Navicat Premium 连oralce报错:oracle library is not loaded
我装的Navicat Premium是64位的,计算机也是64位的,故在oracle网上下了一个64位的instantclient-basic-windows.x64-11.2.0.4.0.zip 地 ...
- Docker Gitlab CI 部署 Spring Boot 项目
目前在学习这一块的内容,但是可能每个人环境都不同,导致找不到一篇博客能够完全操作下来没有错误的,所以自己也写一下,记录一下整个搭建的过程. Docker 的安装这里就不赘述了,基本上几行命令都可以了, ...
- Servlet与Tomcat运行示例
Servlet与Tomcat运行示例 本文将写一个servlet,然后将其部署到Tomcat的全过程.本文参考<深入拆解Tomcat_Jetty>内容. 一.基于web.xml开发步骤 下 ...
- Mybaits-从零开始-Hello World(暂不考虑命名规范化)
1.mybatis-config.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ...
- Marshmallow权限使用
Google发布Android 6.0后对用权限的控制更加严格,在Android5.1或更低的版本中用户能在App的安装期间或使用设置应用程序权限来同意或拒绝某个权限,而在Android6.0或更高的 ...
- 获取配置文件中key=value
之前一直是写一个方法获取配置文件中的key=value值得,现在提供更简单的. ResourceBundle 是java.utl中的一个专门针对.properties文件的. //获取配置文件对象 R ...
- xpath语法分享
# xpath语法: ## 使用方式: 使用//获取整个页面当中的元素,然后写标签名,然后再写谓词进行提取.比如: ``` //div[@class='abc'] ``` ## 需要注意的知识点: 1 ...
- JDK 1.8 之 Map.merge()
Map 中ConcurrentHashMap是线程安全的,但不是所有操作都是,例如get()之后再put()就不是了,这时使用merge()确保没有更新会丢失. 因为Map.merge()意味着我们可 ...
- Java 学习笔记之 线程interrupted方法
线程interrupted方法: interrupted()是Thread类的方法,用来测试当前线程是否已经中断. public class InterruptThread extends Threa ...
- Zeppelin 学习笔记之 Zeppelin安装和elasticsearch整合
Zeppelin安装: Apache Zeppelin提供了web版的类似ipython的notebook,用于做数据分析和可视化.背后可以接入不同的数据处理引擎,包括spark, hive, taj ...