epoll 或 kqueue 是否可以处理向自身异步添加文件描述符

Can epoll or kqueue handle asynchronous additions of file descriptors to itself

如果一个线程(比如 X)正在等待 epoll_wait(),另一个线程(比如 Y)可以调用 epoll_ctl() 来注册对文件描述符 9 的兴趣。之前在线程X中调用epoll_wait() return 线程Y添加的文件描述符9可以吗?对 epoll_wait() 的初始调用在任何时候都不会 return 在中间。

现在我想对此进行比较,并就操作系统中的其他两个轮询调用提出相关问题。 poll()kqueue

  1. 如果上述问题的答案是正确的,那么有没有办法通过 poll() 系统调用实现类似的行为?
  2. 让我们假设 epoll_ctl() 是线程安全的并且线程 X 可以安全地调用 epoll_ctl() 并调用 epoll_wait() return 是否文件描述符 9 已准备好迎接 I/O。将声明对文件描述符感兴趣的函数与要等待的函数分开,这将使该函数令人惊奇。但是人们经常提到 kqueueepoll 用于相同的功能。但是 kqueue 没有单独的函数来声明对获取描述符的事件反馈感兴趣。有人知道如何以与 epoll 类似的方式使用 kqueue 吗? epoll 似乎是目前最好的线程安全选项,如果它允许线程安全 "interest declaration"

来自man epoll_wait

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.

因此 epoll_wait 在等待时监视添加的文件描述符。

poll()/select() 无法实现这种行为,因为它们读取文件描述符集一次,因此无法修改当前轮询的文件描述符。

[当然,如果您将 epoll_create 创建的文件描述符传递给 poll()/select(),将像 epoll_wait 一样跟踪此文件描述符的修改。]