问题描述
我们最近正在阅读BEAM book,这是我们阅读小组的一部分。在Chapter 7中,提到了Erlang中的ref技巧/ Synchronous Call Trick。
不幸的是,这本书是不完整的,经过讨论我们无法确定参考技巧是什么。
解决方法
执行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.