eventfd_read() 什么时候会阻塞?

When will eventfd_read() block?

不知道什么情况下eventfd_read()会阻塞?

我阅读了联机帮助页,但没有提及任何内容。

我通过 eventfd(0,0) 创建了文件描述符。

提前致谢。

来自 eventfd(2) 手册页 read() 调用:

If the eventfd counter is zero at the time of the call to read(2), then the call either blocks until the counter becomes nonzero (at which time, the read(2) proceeds as described above) or fails with the error EAGAIN if the file descriptor has been made nonblocking.

并且对于 eventfd_read()eventfd_write() 函数:

The functions perform the read and write operations on an eventfd file descriptor, returning 0 if the correct number of bytes was transferred, or -1 otherwise.

所以 eventfd_read() 只是 read() 的包装器,当 read() 阻塞时阻塞,即当 eventfd 计数器为零且未设置 O_NONBLOCK 时描述符(使用 fcntl(2)EFD_NONBLOCK)。

您可以在 glibc sources 中验证这一点:

int
eventfd_read (int fd, eventfd_t *value)
{
  return __read (fd, value, sizeof (eventfd_t)) != sizeof (eventfd_t) ? -1 : 0;
}