【发布时间】:2020-12-12 06:02:45
【问题描述】:
我们最近在阅读BEAM book 作为我们阅读小组的一部分。在Chapter 7 中提到了 Erlang 中的 ref 技巧/同步调用技巧。
很遗憾,这本书不完整,经过讨论我们无法弄清楚参考技巧是什么。
【问题讨论】:
我们最近在阅读BEAM book 作为我们阅读小组的一部分。在Chapter 7 中提到了 Erlang 中的 ref 技巧/同步调用技巧。
很遗憾,这本书不完整,经过讨论我们无法弄清楚参考技巧是什么。
【问题讨论】:
在执行receive 时,BEAM VM 会扫描邮箱以找到第一个合适(匹配)的邮件,如果找不到,则阻止该进程。
“诀窍”是,由于在创建新引用之前不可能在邮箱中存在新引用,因此在 receive {Reference, Term} 时无需扫描整个邮箱,只需从创建 Reference 的那一点开始。
这就是下面这句话的意思:The compiler recognizes code that uses a newly created reference (ref) in a receive (see [ref_trick_code]), and emits code to avoid the long inbox scan since the new ref can not already be in the inbox.
【讨论】: