Note that there are some explanatory texts on larger screens.

plurals
  1. POlinux - Can't get eventfd to work with epoll together
    primarykey
    data
    text
    <p>I'm writing a simple server class based on epoll. In order to wake up <code>epoll_wait()</code>, I decided to use an eventfd. It is said that it is better suited for simple event communication and I agree with that. So I created my event and put a watch on it:</p> <pre><code>_epollfd = epoll_create1(0); if (_epollfd == -1) throw ServerError("epoll_create"); _eventfd = eventfd(0, EFD_NONBLOCK); epoll_event evnt = {0}; evnt.data.fd = _eventfd; evnt.events = _events; if (epoll_ctl(_epollfd, EPOLL_CTL_ADD, _eventfd, &amp;evnt) == -1) throw ServerError("epoll_ctl(add)"); </code></pre> <p>later in the message waiting loop, on a separate thread:</p> <pre><code> int count = epoll_wait(_epollfd, evnts, EVENTS, -1); if (count == -1) { if (errno != EINTR) { perror("epoll_wait"); return; } } for (int i = 0; i &lt; count; ++i) { epoll_event &amp; e = evnts[i]; if (e.data.fd == _serverSock) connectionAccepted(); else if (e.data.fd == _eventfd) { eventfd_t val; eventfd_read(_eventfd, &amp;val); return; } } </code></pre> <p>and, of course, the code that stop the server was:</p> <pre><code>eventfd_write(_eventfd, 1); </code></pre> <p>For reasons that I can't explain, I was unable to wake up the <code>epoll_wait()</code> just by writing to the event. Eventually, this worked in a few debugging sessions.</p> <p>Here is my workaround: knowing that <code>EPOLLOUT</code> will trigger an event every time the fd is available for writing, I changed the stop code to</p> <pre><code>epoll_event evnt = {0}; evnt.data.fd = _eventfd; evnt.events = EPOLLOUT; if (epoll_ctl(_epollfd, EPOLL_CTL_MOD, _eventfd, &amp;evnt) == -1) throw ServerError("epoll_ctl(mod)"); </code></pre> <p>Now it works but It shouldn't be this way.</p> <p>I don't believe this should be any difficult. What have I done wrong?</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload