I have read an article about the Erlang select receive mechanism at the end of the article, there is a conclusion: "messages are moved from the mailbox to the save queue and then back to the mailbox after the matching message arrives". I have tried the example shown in the article, but I couldn't get the same result. Here is my code and my erlang/otp version is 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,[]}]
The article.
This strange behaviour with the visible state of a "save queue" was only true in the interpreted code running in the shell, not in regular compiled modules. In the actual C implementation of receive, there is only one queue with a pointer to keep track of which ones have been scanned so far, and process_info does not show an empty queue during a real receive. The behaviour of the interpreted code was fixed back in R16B01, so nowadays there is no visible difference: https://github.com/erlang/otp/commit/acb8ef5d18cc3976bf580a8e6925cb5641acd401