cmultithreadingsocketsnetwork-programmingepoll

Is epoll thread-safe?


There are two functions in epoll:

  1. epoll_ctl
  2. epoll_wait

Are they thread-safe when I use the same epoll_fd?
What will happen if one thread calls epoll_wait and others call epoll_ctl at the same time?


Solution

  • It is thread-safe, but there isn't much documentation that explicitly states that. See here

    BTW, you can also have multiple threads waiting on a single epoll_fd, but in that case it can get a bit tricky. (I.e. you might want to use edge-triggered EPOLLET or oneshot mode EPOLLONESHOT. See here.)