A "sleep" command without "receive"

Matthias Lang <>
Mon Feb 2 10:04:14 CET 2004


Corrado Santoro writes:

 > I've seen that to wait a timeout the statements I've to use are: 
 >  
 > receive 
 >   after T-> ok 
 > end 

[...]
 > If the sending process sends data before the receiving 
 > process calls timer:sleep, the latter function returns 
 > immediatelly, picking also the message. 

You have misunderstood how 'receive' works and you have somehow
tricked yourself into seeing behaviour which cannot and does not
occur. 

If you write a 'receive' block as above and T is nonzero, it will
*not* return immediately and it will *not* consume any
messages. 'receive' blocks only consume messages which match one of
the patterns in the receive block. Your receive block has no patterns
at all and will not match any messages.

Matthias

%%----------------------------------------------------------------------
%% Program which demonstrates a process going to sleep in a receive
%% block despite having a message in its mailbox.
-module(time).
-export([go/0]).

go() ->
	self() ! experimental_verification_is_often_useful,

        io:fwrite("First block sleeps because it has no patterns\n"),
	receive
	  after 5000 -> ok
	end,

        io:fwrite("Second block does not sleep, it matches the message\n"),
	receive
          X -> matched
	  after 5000 -> ok
	end.



More information about the erlang-questions mailing list