Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A problem of multithread epoll in linux

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

like image 565
Utoah Avatar asked May 17 '11 02:05

Utoah


1 Answers

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.

like image 199
davmac Avatar answered Sep 22 '22 23:09

davmac