libevent(六)事件监听

libevent是如何实现事件监听的呢?

在Linux,libevent的底层实现是epoll,因此实现事件监听的方式就是,把需要监听的fd加入epoll中。

I/O事件

定时器事件

定时器事件没有fd,那么如何监听呢?

这里先看下epoll_wait这个函数

int epoll_wait(int epfd, struct epoll_event *events,
                      int maxevents, int timeout);
// The timeout argument specifies the number of milliseconds that epoll_wait() will block.

libevent采用的方法是:每次运行epoll_wait时,选择最小堆堆顶事件的超时时间与当前时间的差值作为epoll_wait函数的timeout。

信号事件

信号事件也没有fd,那么如何监听呢?可参考

libevent(七)信号事件监听

转载于:https://www.cnblogs.com/gattaca/p/7697797.html

猜你喜欢

转载自blog.csdn.net/weixin_34226182/article/details/93401972