ERLANG wait() 和阻塞

发布于 2024-12-29 03:15:30 字数 194 浏览 4 评论 0原文

以下功能是否会阻塞其运行核心?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

默嘫て 2025-01-05 03:15:30

执行该代码的进程阻塞,当前运行该进程的调度程序不会阻塞。您发布的代码等于产量,但有超时。

该核心的 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.

拧巴小姐 2025-01-05 03:15:30

简短的回答:这只会阻止当前(轻量级)进程,并且不会阻止所有虚拟机。有关更多详细信息,您必须阅读有关 erlang 调度程序的内容。很好的描述来自 Francesco Cesarini 和 Simon Thompson 所著的《并发编程》一书。

...剪...
当一个进程被调度时,它会被分配一些减少†
它被允许执行,每执行一次就会减少一个数量
执行的操作。一旦进程进入接收子句
没有消息匹配或者其减少计数达到
零,它被抢占。只要 BIF 没有被执行,这个
策略导致公平(但不平等)的执行分配
进程之间的时间。
...剪断...

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.

...snip...
When a process is dispatched, it is assigned a number of reductions†
it is allowed to execute, a number which is reduced for every
operation executed. As soon as the process enters a receive clause
where none of the messages matches or its reduction count reaches
zero, it is preempted. As long as BIFs are not being executed, this
strategy results in a fair (but not equal) allocation of execution
time among the processes.
...snip...

挽手叙旧 2025-01-05 03:15:30

没有 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文