选择行为
这可能是一个简单的问题,但我找不到明确的答案。我在 C 代码中有多个线程,其中一个使用 select 等待 n 秒。我的问题是,它是否会阻塞整个进程 n 秒(如 usleep),或者 select 是否仅阻塞调用线程(更像 nanosleep)。 感谢您的回答。
It could probably be a simple question but I couldn't find a clear answer for it. I have multiple threads in c code and one of them uses select to wait for n seconds. The question that I have is that does it blocks the entire process for n seconds (like usleep) or does select blocks only the calling thread (more like nanosleep).
Thanks for the answers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我见过几种实现,其中一个线程在
select
上阻塞,而其他线程继续处理 - 所以,是的,它只阻塞正在运行的线程。(抱歉没有带任何参考资料)
I've seen several implementations in which one thread is blocking on
select
while other threads continue processing - so, yes, it only blocks the running thread.(Sorry for not bringing any references)
select
的 POSIX 规范特别提到“thread”仅在一处,它讨论通过pselect()
恢复调用线程的信号掩码。与其他答案一样,我的经验也表明答案是肯定的,它只会阻塞调用线程。
The POSIX spec for
select
specifically mentions "thread" in only one place, where it talks about restoring the signal mask of the calling thread bypselect()
.As with the other answers, my experience also says the answer is yes, it only blocks the calling thread.
是的。一个草率但仍然相当结论性的测试。
Yes. A sloppy but still pretty conclusive test.