简体   繁体   English

linux中多线程epoll的问题

[英]A problem of multithread epoll in linux

I have a multithread linux program which uses epoll(7). 我有一个使用epoll(7)的多线程linux程序。 The epoll(7) man page says when one of its fds gets closed, this fd will be automatically removed from the epoll set. epoll(7)手册页说当其中一个fds关闭时,这个fd将自动从epoll集中删除。 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. 我的问题是,如果epoll集的fd在一个线程中被关闭,而epoll集在另一个线程中被同时轮询而没有同步。 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. epoll集中的fds由内核维护,因此您是安全的 - 内核处理任何必要的同步。

That said, there remains the possibility that an event on the fd comes in on the other thread just before the fd is closed. 也就是说,在fd关闭之前,fd上的事件仍有可能进入另一个线程。 Thus it might be possible to have an event from an fd which no longer appears to be in the set. 因此,有可能从fd中获得一个不再出现在集合中的事件。 With a carefully designed program this shouldn't cause a problem. 通过精心设计的程序,这不应该导致问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM