首页 > 解决方案 > errorfds vs select 返回值,然后 select() 立即返回?

问题描述

我来维护一个软件,它可以:

  /*... init, setting timeout, etc ... */

  FD_ZERO(&set);
  FD_SET(socket_, &set);

  int selectRes = select(socket_ + 1, &set, NULL, NULL, &timeout);
  if (selectRes < 0) {
    throw IoException("Select: ", errno);
  }

  if (selectRes == 0) {
    throw TimeoutException();
  }

  /* ... then handle recvfrom, throw IoException if return < 0 ... */

IoException 应该导致程序终止。超时异常恢复操作。无异常循环返回。socket_ 是一个 UNIX 数据报套接字(从另一个本地进程读取消息)。

This program runs at very high priority (required to react to messages quickly) but it's expected to be idle most of the time, hanging on select()'s timeout waiting for incoming messages. Meanwhile, it seems like it sometimes hogs 100% of CPU time (without receiving enough messages to grant such behavior). The occurrence is rather erratic, never mind the program's high priority makes debugging it very hard (a small single-core Linux embedded system, everything else grinds to a halt).

I'm worried about the NULL in the errorfds position - is testing the return value of select() enough in this case, or may select() return immediately (with 0) if there's an error condition on the socket but errorfds is NULL, and keep repeating doing so every time it loops back to select()?

或者,除了大量消息之外,还有哪些其他情况可以使 select() 立即退出(或者可能在自旋锁中等待而不是释放 CPU 时间)?

标签: c++exceptionselecttimeoutunix-socket

解决方案


推荐阅读