问题
I don't understand how the EVLOOP_NO_EXIT_ON_EMPTY flag is supposed to work in version 2.1.x of libevent.
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.
Goal:
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 :/
Thanks for any help!
回答1:
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);
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.
回答2:
Just use event_base_dispatch (ev_base);
instead of event_base_loop ()``, it will loop and handle all attached events either until you explicitly delete all attached events or call
event_base_loopbreak ()``` function.
来源:https://stackoverflow.com/questions/19757003/libevent-evloop-no-exit-on-empty-not-working