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

Raimo Niskanen raimo+erlang-questions@REDACTED
Wed Dec 5 08:49:11 CET 2007


On Tue, Dec 04, 2007 at 09:40:54PM -0800, Lev Walkin wrote:
> Jack Orenstein wrote:
> > Suppose I have a gen_server process whose queue contains {a} and {b}.  
> > Does the order of the handle_call function clauses determine which  
> > message is processed first? I.e., if the function clauses are
> > 
> >      handle_call({a}, From, State) -> ...
> >      handle_call({b}, From, State) -> ...
> > 
> > is it guaranteed that {a} will be processed first?
> 
> Yes, but this is implementation detail and is not guaranteed
> by the standard.
> 

No, that was incorrect!

The generic gen_server code will receive using a catchall:
	Msg -> Result = Module:handle_call(Msg, From, State)
so messages are received in arrival order. And that is
not an implementation detail (at least it will not change).

The handle_call(_, _, _) clauses then match the patterns in order
(for the currently received message) and the first matching
clause is taken, and that is not an implementation detail.

In fact all messages are received like this (read the code):
    Msg = receive
	      Input ->
		    Input
	  after Time ->
		  timeout
	  end,
Even System messages does not take precedence. That may be
an implementation detail. (There are system messages
used by 'sys' for debugging)

To write a gen_server'ish process with a hand carved
receive statement; use 'proc_lib' and 'sys'. See the
man pages and also OTP's Design Principles in the
documentation. Esp. chapter 6.2.1 Example in
section 6 Sys and Proc_Lib.



> -- 
> Lev Walkin
> vlm@REDACTED
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions

-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB



More information about the erlang-questions mailing list