#include <stdio.h> #include "event2/event.h" #include <unistd.h> void stdin_callback(evutil_socket_t fd,short what,void *arg) { static int i = 0; char buf[1024]; struct event* base = (struct event_base *)arg; printf("stdin_callback start\n"); int ret = read(fd,buf,sizeof(buf)); buf[ret] = '\0'; printf("buf %s\n",buf); printf("stdin_callback end\n"); if(++i >=1) { //event_base_loopbreak(base); //event_base_loopexit(base,NULL); struct timeval timeout = {5,0}; event_base_loopexit(base,&timeout); //5秒之后退出 } } int main() { struct event_base *base = event_base_new(); struct event *ev0 = event_new(base,STDIN_FILENO,0,NULL.NULL); event_assign(ev0,base,STDIN_FILENO,EV_READ|EV_PERSIST,stdin_callback,base); event_add(ev0,NULL); struct event *ev1 = event_new(base,STDIN_FILENO,0,NULL.NULL); event_assign(ev1,base,STDIN_FILENO,EV_READ|EV_PERSIST,stdin_callback,base); event_add(ev1,NULL); /*event loop*/ event_base_loop(base,0);//效果等同于 event_base_dispatch(base); //event_base_loop(base,EVLOOP_ONCE); //event_base_loop(base,EVLOOP_NONBLOCK); if(event_base_got_break(base)) puts("event base loop break"); else if(event_base_got_exit(base)) puts("event base loop exit"); event_base_free(base); event_free(ev0); event_free(ev1); return 0; }
libevent 事件循环
翻译上一篇:libevent 事件状态改变
下一篇:C++ const
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
【Libevent】Libevent学习笔记(三):事件循环
00. 目录文章目录00. 目录01. event_base_loop函数02. event_base_dispatch函数03. event_base_loopexit函数04. event_base_loopbreak函数05. event_base_got_exit函数06. event_base_got_break函数07. event_base_dump_events函数08. eve...
libevent event_base_loopbreak event_base_loop event_base_dispatch event_base_loopexit -
libevent(七)事件循环退出
#include <
c++ #endif #include #ifndef -
libevent (三)libevent 事件处理
=
linux 运维 服务器 事件处理 #endif -
Unable to use libevent (libevent check failed)
yum install libevent-devel -y
libevent-devel Unable libevent linux -
libevent架构 libevent原理
libevent是什么 libevent 库实际上没有更换 select()、poll() 或其他机制的基础。而是使用对于每个平台最高效的高性能解决方案在实现外加上一个包装器。 &nb
libevent架构 libevent linux C/C++ 回调函数 -
UI事件循环机制 android vue事件循环
nextTick的由来: 由于VUE的数据驱动视图更新,是异步的,即修改数据的当下,视图不会立刻更新,而是等同一事件循环中的所有数据变化完成之后,再统一进行视图更新。
UI事件循环机制 android 事件循环 用例 执行栈