[erlang-questions] Order of message processing by a gen_server

Matthew Dempsky <>
Wed Dec 5 08:19:26 CET 2007


On 12/4/07, Jack Orenstein <> wrote:
> Normally this server will receive messages phase1, phase2, phase3 in
> order. But if a cancel message shows up, e.g. after phase2, it takes
> priority (if I understand receive correctly).

No, message ordering takes priority over pattern ordering.  See
section 6.10 of the Erlang Reference Manual for where this is defined,
and see the example module below as a test case.

Eshell V5.5.5  (abort with ^G)
1> c(jao).
{ok,jao}
2> jao:test().
phase1
phase2
phase3
cancel
ok
3>


-module(jao).
-export([test/0]).

test() ->
    self() ! phase1,
    self() ! phase2,
    self() ! phase3,
    self() ! cancel,
    loop().

loop() ->
    receive
        cancel -> io:format("cancel~n"), loop();
        phase1 -> io:format("phase1~n"), loop();
        phase2 -> io:format("phase2~n"), loop();
        phase3 -> io:format("phase3~n"), loop()
    after 0 ->
        ok
    end.



More information about the erlang-questions mailing list