4

Assuming I have the epoll instance mEPoll, can I safely add (via EPOLL_CTL_ADD) or remove (via EPOLL_CTL_DEL) a socket handle via

struct epoll_event event;
event.data.fd = newFD;
event.events = eventmask;
epoll_ctl(mEPoll, EPOLL_CTL_ADD, newFD, &event);

...simultaneously from multiple threads, or do I need to add my own synchronisation primitive around epoll_ctl?

EDIT:

It has been suggested that this question answers my question here. However, that question seems to focus on whether it is safe to call epoll_ctl whilst waiting on epoll_wait: I already know that is safe. This question is purely asking about whether I can simultaneously make two calls to epoll_ctl and not run into problems.

Community
  • 1
  • 1
Wad
  • 1,454
  • 1
  • 16
  • 33
  • I'm actually surprised that I can't find anything about it in the manpages now that you ask, but I've always assumed that it is, and written programs accordingly, because what would be the alternative? It's not like the kernel can allow itself to let its internal epoll object data become ill-defined just because from user-space requests. – Dolda2000 Feb 24 '17 at 15:25
  • Thanks, and I agree. However I'd really like some concrete proof stating that its thread safe... – Wad Feb 24 '17 at 15:30
  • 2
    Looking at the kernel source code, it does certainly hold a mutex inside `epoll_ctl` around all the modifications being done to the internal structures. That doesn't translate to a strict external contract, of course, but nevertheless. – Dolda2000 Feb 24 '17 at 15:32
  • Possible duplicate of [Is epoll thread-safe?](http://stackoverflow.com/questions/7058737/is-epoll-thread-safe) – Stargateur Feb 24 '17 at 16:32
  • I disagree, this question is different: see my edit please. – Wad Feb 24 '17 at 17:15

0 Answers0