版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/guangyacyb/article/details/85251606
安装
环境:ubuntu
tar -zxf libev-4.25.tar.gz
cd libev-4.25
./configure
make
make install
如果权限不够,在前面加sudo即可
简单使用
代码目录:
.
|-- libev-4.25
|-- makefile
`-- test.cpp
test.cpp:
#include <ev.h>
#include <stdio.h>
//ev_io是io事件监听器,ev_timer为定时事件监听器
ev_io stdin_watcher;
ev_timer timeout_watcher;
// ev_io事件的回调函数
static void stdin_cb (EV_P_ ev_io *w, int revents)
{
puts ("stdin ready");
ev_io_stop (EV_A_ w);
ev_break (EV_A_ EVBREAK_ALL);
}
// 定时事件的回调函数
static void timeout_cb (EV_P_ ev_timer *w, int revents)
{
puts ("timeout");
ev_break (EV_A_ EVBREAK_ONE);
}
int main (void)
{
//定义event loop
struct ev_loop *loop = EV_DEFAULT;
// 将标准输入的可读事件和io回调函数绑定到io事件监听器
ev_io_init (&stdin_watcher, stdin_cb,/*STDIN_FILENO*/ 0, EV_READ);
ev_io_start (loop, &stdin_watcher);
// 将超时时间和时间回调函数绑定到定时事件监听器 间隔时间,是否重复
ev_timer_init (&timeout_watcher, timeout_cb, 5.5,0.);
ev_timer_start (loop, &timeout_watcher);
ev_run (loop, 0);
return 0;
}
makefile:
target=main
$(target):test.cpp
g++ -o $@ $^ -L/usr/local/lib/ -lev
clean:
rm -rf $(target) *.o
编译为可执行文件后并执行,等待5.5s或者回车都会使程序结束。
参考: