Is epoll thread-safe?

atomd picture atomd · Aug 14, 2011 · Viewed 11.9k times · Source

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?

Answer

cmeerw picture cmeerw · Aug 14, 2011

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.)