ERLANG wait() 和阻塞
以下功能是否会阻塞其运行核心?
wait(Sec) ->
receive
after (1000 * Sec) -> ok
end.
一个很好的答案将详细介绍 Erlang 和/或 CPU 的内部工作。
Does the following function block on its running core?
wait(Sec) ->
receive
after (1000 * Sec) -> ok
end.
A great answer will detail the internal working of Erlang and/or the CPU.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
执行该代码的进程将阻塞,当前运行该进程的调度程序不会阻塞。您发布的代码等于产量,但有超时。
该核心的 Erlang VM 调度程序将继续执行其他进程,直到超时触发并且该进程将被再次调度执行。
The process which executes that code will block, the scheduler which runs that process currently will not block. The code you posted is equal to a yield, but with a timeout.
The Erlang VM scheduler for that core will continue to execute other processes until that timeout fires and that process will be scheduled for execution again.
简短的回答:这只会阻止当前(轻量级)进程,并且不会阻止所有虚拟机。有关更多详细信息,您必须阅读有关 erlang 调度程序的内容。很好的描述来自 Francesco Cesarini 和 Simon Thompson 所著的《并发编程》一书。
Short answer: this will block only current (lightweight) process, and will not block all VM. For more details you must read about erlang scheduler. Nice description comes from book "Concurent Programming" by Francesco Cesarini and Simon Thompson.
没有 Erlang 特有的、非常经典的问题:超时只能发生在系统时钟中断上。与上面相同的答案:该进程被阻塞等待时钟中断,其他一切都工作正常。
还有另一个关于进程将等待的实际时间的讨论,这并不那么精确,因为它取决于时钟周期(并且取决于系统),但这是另一个主题。
nothing Erlang-specific, pretty classical problem: timeouts can only happen on a system clock interrupt. Same answer as above: that process is blocked waiting for the clock interrupt, everything else is working just fine.
There is another discussion about the actual time that process is going to wait which is not that precise exactly because it depends on the clock period (and that's system dependent) but that's another topic.