Curious behaviour of gen_tcp

Torbjorn Tornkvist tobbe@REDACTED
Wed Jan 31 10:10:32 CET 2001


When doing pattern matching in a receive clause,
Erlang takes the first message in the queue and
tries to match it agains any of the receive clauses
(in top-down order). See attached example.

However, if this can happend in your gen_tcp case I don't know.

Cheers /Tobbe
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-module(aa).
-export([bb/0]).

%%%
%%% See how messages are received:
%%%
%%% 1> P=aa:bb().
%%% <0.28.0>
%%% 2> P! tcp.
%%% tcp
%%% 3> P! tcp.
%%% tcp
%%% 4> P! tcp_closed.
%%% tcp_closed
%%% 5> P! tcp.       
%%% tcp
%%% 6> P! tcp.
%%% tcp
%%% 7> P! start.
%%% start
%%% got tcp
%%% got tcp
%%% got tcp_closed
%%% got tcp
%%% got tcp
%%% 8> 
%%%

bb() -> spawn(fun() -> init() end).

init() ->
    receive start -> true end,
    loop().

loop() ->
    receive
	tcp -> 
	    io:format("got tcp~n"),
	    loop();

	tcp_closed -> 
	    io:format("got tcp_closed~n"),
	    loop()
    end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



More information about the erlang-questions mailing list