zoukankan      html  css  js  c++  java
  • libev 简单用法示例

    ubuntu下安装libev开发包:

    $ sudo apt-get install libev-dev

    下面是libev的man page自带的libev用法示例,保存到testlibev.c

     1 // a single header file is required
     2 #include <ev.h>
     3 
     4 #include <stdio.h> // for puts
     5 
     6 // every watcher type has its own typedef’d struct
     7 // with the name ev_TYPE
     8 ev_io stdin_watcher;
     9 ev_timer timeout_watcher;
    10 
    11 // all watcher callbacks have a similar signature
    12 // this callback is called when data is readable on stdin
    13 static void stdin_cb (EV_P_ ev_io *w, int revents)
    14 {
    15     puts ("stdin ready");
    16     // for one-shot events, one must manually stop the watcher
    17     // with its corresponding stop function.
    18     ev_io_stop (EV_A_ w);
    19 
    20     // this causes all nested ev_run’s to stop iterating
    21     ev_break (EV_A_ EVBREAK_ALL);
    22 }
    23 
    24 // another callback, this time for a time-out
    25 static void timeout_cb (EV_P_ ev_timer *w, int revents)
    26 {
    27     puts ("timeout");
    28     // this causes the innermost ev_run to stop iterating
    29     ev_break (EV_A_ EVBREAK_ONE);
    30 }
    31 
    32 int main (void)
    33 {
    34     // use the default event loop unless you have special needs
    35     struct ev_loop *loop = EV_DEFAULT;
    36 
    37     // initialise an io watcher, then start it
    38     // this one will watch for stdin to become readable
    39     ev_io_init (&stdin_watcher, stdin_cb, /*STDIN_FILENO*/ 0, EV_READ);
    40     ev_io_start (loop, &stdin_watcher);
    41 
    42     // initialise a timer watcher, then start it
    43     // simple non-repeating 5.5 second timeout
    44     ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.);
    45     ev_timer_start (loop, &timeout_watcher);
    46 
    47     // now wait for events to arrive
    48     ev_run (loop, 0);
    49 
    50     // unloop was called, so exit
    51     return 0;
    52 }

    编译:

    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的函数调用:
  • 相关阅读:
    项目管理__项目验收材料
    机器学习----机器学习概述、机器学习的应用及典型相关算法
    机器学习--线性回归
    centos7.2密码在单用户下面的修改
    网络故障排查
    业务数据模型短暂思考整理
    mysql--->安装
    powershell
    常见面试题
    接口
  • 原文地址:https://www.cnblogs.com/zhangzhang/p/2848872.html
Copyright © 2011-2022 走看看