我正在用C ++编写一个多线程服务器。主事件循环如下所示:
for (;loop_enabled_;)
{
int ret = epoll_wait(epollfd, events, epoll_event_buf_size(), -1);
if (ret >= 0)
handle_events(events, events + ret);
else
{
std::error_code ec(errno, std::system_category());
logger->critical("Error in epoll_wait: {}", ec.message());
throw std::system_error(ec);
}
}
但是,在极少数情况下epoll_wait
可能会失败Bad file descriptor,即使它已成功完成多次迭代。
根据man epoll_wait
:
错误
EBADF epfd is not a valid file descriptor. EFAULT The memory area pointed to by events is not accessible with write permissions. EINTR The call was interrupted by a signal handler before either (1) any of the requested events occurred or (2) the timeout expired; see signal(7). EINVAL epfd is not an epoll file descriptor, or maxevents is less than or equal to zero.
EBADF
建议epollfd
在我的代码中无效,但由于它已完成多次迭代,invalid
在这里意味着什么?在close
之前epoll_ctl(EPOLL_CTL_DEL)
会导致此吗?