Erlang中的空进程邮箱

Muz*_*hua 4 erlang message-passing

当您向shell进程发送消息时,可以通过调用以下命令清除所有消息: c:flush().

C:\Windows\System32>erl
Eshell V5.9  (abort with ^G)
1> self() ! josh.
josh
2> self() ! me.
me
3> self() ! you.
you
4> flush().
Shell got josh
Shell got me
Shell got you
ok
5>

在我的想法中,这会清空shell进程的邮箱.清空任何erlang进程邮箱的等效方法是什么?

ste*_*emm 8

此函数应刷新邮箱中的所有邮件(在您调用它的任何进程中):

flush() ->
        receive
                _ -> flush()
        after
                0 -> ok
        end.
Run Code Online (Sandbox Code Playgroud)

  • 是的,`after 0 - >`保证在"超时"之前尝试匹配邮箱中的所有邮件.它比`after 1 - >`更有效,因为实际上没有启动计时器; 它不是必需的. (4认同)