Erlang选择接收机制的详细信息是什么?

ivqmmu1c  于 2022-12-08  发布在  Erlang
关注(0)|答案(1)|浏览(173)

我看过一篇关于Erlang select接收机制的文章,在文章的最后有一个结论:“邮件从邮箱移到保存队列,然后在匹配的邮件到达后移回邮箱”。我已经尝试了文章中显示的例子,但我无法得到相同的结果。这是我的代码,我的erlang/otp版本是21。

shell1:
(aaa@HW0003727)1> register(shell, self()).
true
(aaa@HW0003727)2> shell ! c, shell ! d.
d
(aaa@HW0003727)3> process_info(whereis(shell),messages).
{messages,[c,d]}.
(aaa@HW0003727)4> receive a -> 1; b -> 2 end.

shell2:
(aaa@HW0003727)1> process_info(whereis(shell),messages).
{messages,[c,d]}
(aaa@HW0003727)2> process_info(whereis(shell)).          
[{registered_name,shell},
 {current_function,{prim_eval,'receive',2}},
 {initial_call,{erlang,apply,2}},
 {status,waiting},
 {message_queue_len,2},
 {links,[<0.113.0>]},
 {dictionary,[]},
 {trap_exit,false},
 {error_handler,error_handler},
 {priority,normal},
 {group_leader,<0.112.0>},
 {total_heap_size,4212},
 {heap_size,1598},
 {stack_size,30},
 {reductions,13906},
 {garbage_collection,[{max_heap_size,#{error_logger => true,kill => true,size => 0}},
                      {min_bin_vheap_size,46422},
                      {min_heap_size,233},
                      {fullsweep_after,65535},
                      {minor_gcs,1}]},
 {suspending,[]}]

article

093gszye

093gszye1#

这种带有“保存队列”可见状态的奇怪行为只在shell中运行的解释代码中才是真实的,而在常规编译的模块中则不是。在receive的实际C实现中,只有一个队列带有一个指针来跟踪到目前为止哪些队列已经被扫描过,并且process_info在真实的接收期间不会显示空队列。解释代码的行为在R16B01中已修复,因此现在没有明显的差异:https://github.com/erlang/otp/commit/acb8ef5d18cc3976bf580a8e6925cb5641acd401

相关问题