libev简单使用介绍

更详细的用法请看他的文档:

http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod

struct ev_loop *ev_default_loop (unsigned int flags)
struct ev_loop *ev_loop_new (unsigned int flags)


这两个函数都是默认初始化一个loop,区别是第一个不是线程安全的,第二个不能捕捉信号和子进程的watcher。

参数flags可以为下面几种类型:

[quote]#define EVFLAG_AUTO 0x00000000U /* not quite a mask */
/* flag bits */
#define EVFLAG_NOENV 0x01000000U /* do NOT consult environment */
#define EVFLAG_FORKCHECK 0x02000000U /* check for a fork in each iteration */
/* method bits to be ored together */
#define EVBACKEND_SELECT 0x00000001U /* about anywhere */
#define EVBACKEND_POLL 0x00000002U /* !win */
#define EVBACKEND_EPOLL 0x00000004U /* linux */
#define EVBACKEND_KQUEUE 0x00000008U /* bsd */
#define EVBACKEND_DEVPOLL 0x00000010U /* solaris 8 */ /* NYI */
#define EVBACKEND_PORT 0x00000020U /* solaris 10 */[/quote]

ev_default_fork ()
ev_loop_fork (loop)


这两个函数就是当你在子进程里需要使用libev的函数的之前必须要调用。他们的区别是第二个函数是当使用ev_loop_new创建的loop时,才用第二个函数,也就是说重用父进程创建的loop。

ev_loop (loop, int flags)



开始事件循环。


ev_TYPE_init (ev_TYPE *watcher, callback, [args])


初始化一个watcher。TYPE也就是libev支持的事件类型,比如io,比如time等等。。

第一个参数为一个watcher,第二个回调函数,第三个句柄,第四个事件类型。包含下面几种:

[quote]#define EV_UNDEF -1 /* guaranteed to be invalid */
#define EV_NONE 0x00 /* no events */
#define EV_READ 0x01 /* ev_io detected read will not block */
#define EV_WRITE 0x02 /* ev_io detected write will not block */
#define EV_IOFDSET 0x80 /* internal use only */
#define EV_TIMEOUT 0x00000100 /* timer timed out */
#define EV_PERIODIC 0x00000200 /* periodic timer timed out */
#define EV_SIGNAL 0x00000400 /* signal was received */
#define EV_CHILD 0x00000800 /* child/pid had status change */
#define EV_STAT 0x00001000 /* stat data changed */
#define EV_IDLE 0x00002000 /* event loop is idling */
#define EV_PREPARE 0x00004000 /* event loop about to poll */
#define EV_CHECK 0x00008000 /* event loop finished poll */
#define EV_EMBED 0x00010000 /* embedded event loop needs sweep */
#define EV_FORK 0x00020000 /* event loop resumed in child */
#define EV_ASYNC 0x00040000 /* async intra-loop signal */
#define EV_ERROR 0x80000000 /* sent when an error occurs */[/quote]

[quote]ev_TYPE_start (loop *, ev_TYPE *watcher)[/quote]

启动一个watcher。

例子:

  #include <ev.h>
#include <stdio.h>

//不同的watcher
ev_io stdin_watcher;
ev_timer timeout_watcher;
ev_timer timeout_watcher_child;

//标准输入的回调函数
static void
stdin_cb (EV_P_ ev_io *w, int revents)
{
puts ("stdin ready");

ev_io_stop (EV_A_ w);

ev_unloop (EV_A_ EVUNLOOP_ALL);
}

//父进程的定时器回调函数
static void
timeout_cb (EV_P_ ev_timer *w, int revents)
{
puts ("timeout");
ev_unloop (EV_A_ EVUNLOOP_ONE);
}
//子进程的定时器回调函数
static void
timeout_cb_child (EV_P_ ev_timer *w, int revents)
{
puts ("child timeout");
ev_unloop (EV_A_ EVUNLOOP_ONE);
}
int
main (void)
{
//创建一个backend为select的loop
struct ev_loop *loop = ev_loop_new(EVBACKEND_SELECT);

//初始化并启动父进程的watcher
ev_timer_init(&timeout_watcher, timeout_cb, 10, 0.);
ev_timer_start(loop, &timeout_watcher);
switch (fork()) {
case -1:
return -1;
case 0:
//使用父进程loop。
ev_loop_fork(loop);
//子进程的loop
struct ev_loop *loop_child = ev_loop_new (EVBACKEND_SELECT);
ev_io_init (&stdin_watcher, stdin_cb, /*STDIN_FILENO*/ 0, EV_READ);
ev_io_start (loop, &stdin_watcher);
ev_timer_init(&timeout_watcher_child, timeout_cb_child, 5.5, 0.);
ev_timer_start(loop_child, &timeout_watcher_child);
ev_loop(loop_child,0);
}

//等待事件
ev_loop (loop, 0);
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值