A "sleep" command without "receive"

Matthias Lang matthias@REDACTED
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

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.


%% Program which demonstrates a process going to sleep in a receive
%% block despite having a message in its mailbox.

go() ->
	self() ! experimental_verification_is_often_useful,

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

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

More information about the erlang-questions mailing list