A problem of multithread epoll in linux
Asked Answered
L

1

7

I have a multithread linux program which uses epoll(7). The epoll(7) man page says when one of its fds gets closed, this fd will be automatically removed from the epoll set. My question is what if a fd of the epoll set gets closed in one thread while the epoll set is being polled in another thread concurrently without synchronization. Will the program get corrupted or will the kernel synchronize this access automatically?

Thanks

Feng

Logistician answered 17/5, 2011 at 2:52 Comment(0)
G
7

The fds in an epoll set are maintained by the kernel, so you are safe - the kernel handles any necessary synchronization.

That said, there remains the possibility that an event on the fd comes in on the other thread just before the fd is closed. Thus it might be possible to have an event from an fd which no longer appears to be in the set. With a carefully designed program this shouldn't cause a problem.

Gerhardt answered 17/5, 2011 at 5:29 Comment(1)
+1 FWIW on my linux 2.6 test box, close()ing the read fd of a pipe while it is being epoll_wait()ed appears to silently remove the fd from the poll set. So, as a worst case, if the epoll_wait() is indefinite, and the close()d fd was the only fd or the only one guaranteed to wake up the process, you might hang forever.Schleswigholstein

© 2022 - 2024 — McMap. All rights reserved.