论坛首页 编程语言技术论坛

libev简单使用介绍

浏览 5621 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-12-30  
C
更详细的用法请看他的文档:

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可以为下面几种类型:

引用
#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 */


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,第二个回调函数,第三个句柄,第四个事件类型。包含下面几种:

引用
#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 */


引用
ev_TYPE_start (loop *, ev_TYPE *watcher)


启动一个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;
   }

论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics