libevent EVLOOP_NO_EXIT_ON_EMPTY无法正常工作吗? [英] libevent EVLOOP_NO_EXIT_ON_EMPTY not working?
问题描述
我不明白EVLOOP_NO_EXIT_ON_EMPTY标志在libevent的2.1.x版本中应该如何工作.
I don't understand how the EVLOOP_NO_EXIT_ON_EMPTY flag is supposed to work in version 2.1.x of libevent.
如果我未在event_base中添加任何事件,则
If I don't add any events to my event_base the
event_base_loop(my_base, EVLOOP_NO_EXIT_ON_EMPTY);
通话返回 立即,这根本不是我认为应该做的.
call returns immediately which is not at all what I think it's supposed to do.
如果我添加一个事件,它将与该未决事件一起循环,直到该事件变为活动状态为止,然后退出该循环,而我希望不会发生这种情况.
If I add an event it loops with that pending event until it get's active but then the loop exits which I hoped would not happen.
目标:
打开一个命名管道,并在libevent中侦听读取. 每当我
Have a named pipe open and libevent listening for a read. Whenever I
echo "something" > pipe
应调用已注册的回调.如果回调已完成,则事件返回到待处理状态,循环将等待另一个回声.
the registered callback should be called. If the callback has finished the event get's back to pending and the loop waits for another echo.
这是我到目前为止所得到的:(省略了错误检查)
Here's what I got so far: (error checking omitted)
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <event.h>
#include <unistd.h>
#define PIPE "/tmp/ev_pipe"
void cb_func(evutil_socket_t fd, short what, void *arg)
{
printf("foo\n");
}
int main(void)
{
/* create & open named pipe */
mkfifo(PIPE, 0666);
int socket = open(PIPE, O_RDONLY | O_NONBLOCK);
/* libevent specific stuff */
struct event_base *ev_base = event_base_new();
struct event *ev = event_new(ev_base, (evutil_socket_t) socket, EV_READ, cb_func, NULL);
event_add(ev, NULL);
/* loop forever */
event_base_loop(ev_base, EVLOOP_NO_EXIT_ON_EMPTY);
printf("a\n");
/* clean up */
unlink(PIPE);
event_base_free(ev_base);
close(socket);
return 0;
}
我想念什么?第一次写入队列后,事件循环退出:/
What am I missing? The event loop exits after the first write to the queue :/
感谢您的帮助!
推荐答案
该功能的实现似乎有问题!我在2.1.x版本中也遇到过同样的问题.解决该问题的一种方法是@Wizzard指出.解决该问题的另一种方法是将标志EV_PERSIST与功能event_new的events参数相符:
The implementation of the feature looks buggy! I had faced the same issue with the 2.1.x version. One way to get around the issue is as @Wizzard has pointed out. Another way of getting around the issue is OR the flag EV_PERSIST to the events argument to the function event_new:
struct event *ev = event_new(ev_base,
(evutil_socket_t) socket, EV_READ|EV_PERSIST, cb_func, NULL);
这将防止事件被删除. https://github.com/libevent/libevent/blob/master /include/event2/event.h +872
This will prevent the event from being removed. https://github.com/libevent/libevent/blob/master/include/event2/event.h +872
请注意,管道上有数据时,您可能会得到多个回调.
Please be aware that you might get multiple callbacks when there is data on pipe.
这篇关于libevent EVLOOP_NO_EXIT_ON_EMPTY无法正常工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!