libev 简单用法示例
2013-01-07 11:05
267 查看
ubuntu下安装libev开发包:
$ sudo apt-get install libev-dev
下面是libev的man page自带的libev用法示例,保存到testlibev.c
编译:
gcc testlibev.c -lev -o testlibev
运行:
./testlibev
timeout
./testlibev
a
stdin ready
第一个没有输入,所以等到5.5秒后超时。
第二个输入a,所以stdin监测到事件退出。
首先来看ev_io_init中做了些什么操作:
ev.h文件中
#define ev_io_init(ev,cb,fd,events)
do { ev_init ((ev), (cb)); ev_io_set ((ev),(fd),(events)); } while (0)
#define ev_init(ev,cb_) do { \
((ev_watcher *)(void *)(ev))->active = \
((ev_watcher *)(void *)(ev))->pending = 0; \
ev_set_priority ((ev), 0); \
ev_set_cb ((ev), cb_); \
} while (0)
#define ev_io_set(ev,fd_,events_)
do { (ev)->fd = (fd_); (ev)->events = (events_) | EV__IOFDSET; } while (0)
#define ev_set_cb(ev,cb_)
ev_cb (ev) = (cb_)
#define ev_cb(ev)
(ev)->cb /* rw */
通过定义可以看出ev_io_init主要操作是:
&stdin_watcher->active=stdin_watcher->pending=0;
&stdin_watcher->priority=0;
&stdin_watcher->cb=stdin_cb(函数);
&stdin_watcher->fd;
&stdin_watcher->events=EV_READ|EV__IOFDSET
同样,ev_io_start做了一些赋值操作,这里不过多讲解;
下面通过一张函数调用图来展libev的函数调用:
$ sudo apt-get install libev-dev
下面是libev的man page自带的libev用法示例,保存到testlibev.c
// a single header file is required #include <ev.h> #include <stdio.h> // for puts // every watcher type has its own typedef’d struct // with the name ev_TYPE ev_io stdin_watcher; ev_timer timeout_watcher; // all watcher callbacks have a similar signature // this callback is called when data is readable on stdin static void stdin_cb (EV_P_ ev_io *w, int revents) { puts ("stdin ready"); // for one-shot events, one must manually stop the watcher // with its corresponding stop function. ev_io_stop (EV_A_ w); // this causes all nested ev_run’s to stop iterating ev_break (EV_A_ EVBREAK_ALL); } // another callback, this time for a time-out static void timeout_cb (EV_P_ ev_timer *w, int revents) { puts ("timeout"); // this causes the innermost ev_run to stop iterating ev_break (EV_A_ EVBREAK_ONE); } int main (void) { // use the default event loop unless you have special needs struct ev_loop *loop = EV_DEFAULT; // initialise an io watcher, then start it // this one will watch for stdin to become readable ev_io_init (&stdin_watcher, stdin_cb, /*STDIN_FILENO*/ 0, EV_READ); ev_io_start (loop, &stdin_watcher); // initialise a timer watcher, then start it // simple non-repeating 5.5 second timeout ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.); ev_timer_start (loop, &timeout_watcher); // now wait for events to arrive ev_run (loop, 0); // unloop was called, so exit return 0; }
编译:
gcc testlibev.c -lev -o testlibev
运行:
./testlibev
timeout
./testlibev
a
stdin ready
第一个没有输入,所以等到5.5秒后超时。
第二个输入a,所以stdin监测到事件退出。
首先来看ev_io_init中做了些什么操作:
ev.h文件中
#define ev_io_init(ev,cb,fd,events)
do { ev_init ((ev), (cb)); ev_io_set ((ev),(fd),(events)); } while (0)
#define ev_init(ev,cb_) do { \
((ev_watcher *)(void *)(ev))->active = \
((ev_watcher *)(void *)(ev))->pending = 0; \
ev_set_priority ((ev), 0); \
ev_set_cb ((ev), cb_); \
} while (0)
#define ev_io_set(ev,fd_,events_)
do { (ev)->fd = (fd_); (ev)->events = (events_) | EV__IOFDSET; } while (0)
#define ev_set_cb(ev,cb_)
ev_cb (ev) = (cb_)
#define ev_cb(ev)
(ev)->cb /* rw */
通过定义可以看出ev_io_init主要操作是:
&stdin_watcher->active=stdin_watcher->pending=0;
&stdin_watcher->priority=0;
&stdin_watcher->cb=stdin_cb(函数);
&stdin_watcher->fd;
&stdin_watcher->events=EV_READ|EV__IOFDSET
同样,ev_io_start做了一些赋值操作,这里不过多讲解;
下面通过一张函数调用图来展libev的函数调用:
相关文章推荐
- Django rest framework工具包简单用法示例
- jQuery简单自定义图片轮播插件及用法示例
- AjaxPanel自定义控件实现页面无刷新数据交互(做了个示例程序, 效果确实比较Cool, 用法非常简单! )(示例代码下载)
- RequireJS用法简单示例
- AjaxPanel自定义控件实现页面无刷新数据交互(做了个示例程序, 效果确实比较Cool, 用法非常简单! )(示例代码下载)
- AjaxPanel自定义控件实现页面无刷新数据交互(做了个示例程序, 效果确实比较Cool, 用法非常简单!
- jQuery的each循环用法简单示例
- [导入]AjaxPanel自定义控件实现页面无刷新数据交互(做了个示例程序, 效果确实比较Cool, 用法非常简单! )(示例代码下载)
- trcasst 用法简单示例
- PHP单态模式简单用法示例
- AjaxPanel自定义控件实现页面无刷新数据交互(做了个示例程序, 效果确实比较Cool, 用法非常简单! )(示例代码下载)
- java学习-JAVA基本排序法示例,JAVA数据类型转换 ,ArrayList用法,Calendar 简单应用
- exec 命令简单用法 和 find 搭配用法示例
- 常用的JS与XML结合用法 简单示例
- sql语句中"case when"的简单用法示例
- 微信小程序picker组件简单用法示例【附demo源码下载】
- Java实现的简单数字处理类及用法示例
- Bootstrap模态对话框用法简单示例
- AjaxPanel自定义控件实现页面无刷新数据交互(做了个示例程序, 效果确实比较Cool, 用法非常简单! )(示例代码下载)
- jQuery中可见性过滤器简单用法示例