开发者

A problem of multithread epoll in linux

开发者 https://www.devze.com 2023-03-06 03:59 出处:网络
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开发者_JAVA百科 from the epoll set.My question i

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开发者_JAVA百科 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消