在一个线程中执行epoll_ctl而另一个线程在epoll_wait的中间(Executing epoll_ctl in one thread while the other thread is in the middle of epoll_wait)

我是epoll的linux服务器编程的新手。 我有2个线程:Thread_Accept和Thread_epoll。 前者是块接受循环,如果新连接即将到来,它将使用epoll_ctl()添加新的fd。 后者是一个很大的epoll_wait()循环。

现在我的问题是:如果在Thread_epoll处于epoll_wait中间时Thread_Accept执行epoll_ctl(),该怎么办? 会有副作用吗?

提前致谢。

问候,马丁

I am new to linux server programming with epoll. I have 2 threads: Thread_Accept and Thread_epoll. The former is block accept loop, if new connection is coming, it will add the new fd with epoll_ctl(). The latter is a big epoll_wait() loop.

Now my question is: What if when Thread_Accept executing epoll_ctl() while the Thread_epoll is in the middle of epoll_wait? Will it got any side effect?

Thanks in advance.

regards, Martin

最满意答案

从epoll_wait手册页:

虽然在调用epoll_pwait()时阻塞了一个线程,但是另一个线程可能会将文件描述符添加到等待的epoll实例中。 如果新文件描述符准备就绪,则会导致epoll_wait()调用解除阻塞。

因此,添加新文件描述符时没有副作用:-)

(本手册页注释是nathansizemore提到的bug的结果)

From the epoll_wait man page:

While one thread is blocked in a call to epoll_pwait(), it is possible for another thread to add a file descriptor to the waited-upon epoll instance. If the new file descriptor becomes ready, it will cause the epoll_wait() call to unblock.

So, no side effects when adding a new file descriptor :-)

(This man page note is the result of the bug mentioned by nathansizemore)

更多推荐