receive pitfall

HEINRICH VENTER HEINRICH.VENTER@REDACTED
Tue Nov 25 12:23:36 CET 2003


Hi again List

A pitfall I have stepped in twice now, and I thought I'd share :)

Be careful of a general term in your receive statements!  Your general term WILL catch anything and everything, including messages that might be intended for the same process in a different "state".  This is something you have to be especialy weary of when you have more than one receive statement in the same process, but perhaps in different functions.

For example

a() ->
   receive
      cmd_1 -> b(), a();
      cmd_2 -> a();
      Else -> error
   end.

b() ->
   receive
      stuff -> ok;
      Else -> b()
   end.

In this case if the messages that are sent are cmd_1, cmd_2, stuff it will seem to work correctly, but the cmd_2 message will be lost.  Even worse, if you are trapping EXIT messages, you might throw that away inadvertandly!

I presume the best practice would be to rather use an after clause in a receive than a generic term in case something unexpected happens?

-]-[einrich



#####################################################################################
The information contained in this message and or attachments is intended
only for the person or entity to which it is addressed and may contain
confidential and/or privileged material.  Any review, retransmission,
dissemination or other use of, or taking of any action in reliance upon,
this information by persons or entities other than the intended recipient
is prohibited. If you received this in error, please contact the sender and
delete the material from any system and destroy all copies.
#####################################################################################



More information about the erlang-questions mailing list