libevent-简单的定时器
2016-01-20 21:09
330 查看
/* For sockaddr_in */ #include <netinet/in.h> /* For socket functions */ #include <sys/socket.h> /* For gethostbyname */ #include <netdb.h> #include <unistd.h> #include <string.h> #include <stdio.h> #include <event2/event.h> #include <event2/util.h> static void callback(int,short,void*); static const struct timeval tv={5,0}; int main(int argc, char **argv) { //libevent与事件循环一起工作 struct event_base* ev_base; //创建一个event_base ev_base=event_base_new(); if(!ev_base) { perror("Could not create event base!\n"); return 1; } struct event *cb_event; evutil_socket_t fd; //创建一个循环事件 //由于事件被标记为EV_TIMEOUT,即超时后事件将变为激活的 //同时事件具有EV_PERSIST,意味着即使回调被激活,事件状态还是未决的 cb_event=event_new(ev_base,fd,EV_TIMEOUT|EV_PERSIST,callback,(char*)"event message"); event_add(cb_event,&tv); //开始事件循环 event_base_dispatch(ev_base); //清空事件资源 event_free(cb_event); event_base_free(ev_base); printf("done!\n"); return 0; } static void callback(int fd,short what,void* arg) { static int call_number=0; const char* data=arg; printf("%s%d\n",arg,call_number); ++call_number; }
相关文章推荐
- 小学生测验管理
- Expandablelistview用后详解
- 移动 摇杆的创建
- ASP.NET SignalR 与LayIM配合,轻松实现网站客服聊天室(二) 实现聊天室连接
- 1、什么是Lucene,Lucene能干什么
- poj3087
- Gradle 基本用法
- BOF——Bag-of-Featrures
- HDU 1565:最大点权独立集(网络流)
- android:scaleType属性
- BP神经网络算法
- 使用谷歌Volley和gson序列化,反序列
- 蓝桥杯:入门训练 Fibonacci数列
- 解决Ora-28056问题
- 【Android】intent action 跳转到系统使用,调用系统功能
- Picture(HDOJ-2052)
- 第一天
- 数据结构之递归算法
- HDU 2035 人见人爱A^B
- c语言成绩统计系统