简体   繁体   English

中断accept()

[英]Interrupting accept()

Is there a OS portable way of interrupting a blocking accept ? 是否有OS便携式方法来中断阻止accept I have a multi-threaded deamon/service that needs to gracefully close all listening network hosts when the deamon/service gets a shutdown signal. 我有一个多线程守护进程/服务,当守护进程/服务收到关闭信号时,它需要正常关闭所有侦听网络主机。 I see that some say you should use non-blocking sockets or select with a time-out - but won't these degrade performance as my application should be as fast as possible? 我看到有人说您应该使用非阻塞套接字或使用超时进行选择-但是这些不会降低性能,因为我的应用程序应该尽可能快吗? The deamon runs in the main thread while each of the listening network hosts run in their own thread. 守护进程在主线程中运行,而每个侦听网络主机在其自己的线程中运行。 The problem now is that accept wait indefinitely if there is no network traffic on the listening network host's socket. 现在的问题是,如果侦听网络主机的套接字上没有网络流量,则accept无限期等待。 If I should use signals, then is there an example somewhere of using signals to interrupt accept ? 如果我应该使用信号,那么是否有使用信号中断accept的示例?

The solution here is to not call accept when there's nothing to do. 解决方案是在无事可做时不调用accept Just use non-blocking select or poll to wait until there's something to accept, then accept at that point. 只需使用非阻塞selectpoll来等待,直到有要接受的东西,然后再accept Unless you create a really tiny timeout there won't be any performance implications of waking up from the non-blocking call and going back to wait on that socket again. 除非您创建一个非常小的超时,否则从非阻塞调用中唤醒并再次等待该套接字不会有任何性能影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM