【libev】ev_io


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

struct ev_loop *io_loop = NULL;
ev_io io_watcher;

static void io_cb(EV_P_ ev_io *w, int revents)
{
        char buf[128]={0};
        int count = 0;

        printf("io_cb call\n");
        sleep(1);
        count = read(STDIN_FILENO, buf, 128); // read方法可以清空STDIN_FILENO缓冲区,如果不清空该缓冲区,则会一直触发该回调。
        printf("count = %d, buf = %s\n", count, buf);

        //ev_io_stop(EV_A_ w);

        //ev_break(EV_A_ EVBREAK_ALL);
        //ev_io_init(&io_watcher, io_cb, STDIN_FILENO, EV_READ);
        //ev_io_start(io_loop, &io_watcher);
}

int main()
{
        io_loop = ev_loop_new(EVFLAG_AUTO);
        ev_io_init(&io_watcher, io_cb, STDIN_FILENO, EV_READ);
        ev_io_start(io_loop, &io_watcher);
        ev_run(io_loop, 0); // 如果不在回调中调用stop或者break方法,ev_run后面的代码就永远不会被执行。所以一般会把注册事件放到子线程中去做。

        while(1){printf("abcd");}
        printf("main() call, end\n");
        return 0;
}


猜你喜欢

转载自blog.csdn.net/u011362297/article/details/48518141