[erlang-questions] Completing the thought (a question about receive)

Kushal Kumaran kushal@REDACTED
Mon Feb 18 18:08:23 CET 2019

Hi Donald,

Donald Steven <t6sn7gt@REDACTED> writes:

> In a receive block, will the code 'thought' be completed before things
> goes 'back' to the beginning of the block, or does another incoming
> message received before the completion of that thought 'interrupt' the
> thought.  If I haven't expressed that clearly, consider:

You appear to interpret receive as some kind of a loop construct.  It is
not.  A receive statement receives at most one message that matches one
of the patterns.  Messages received by the process are held in the
mailbox until a receive statement is executed.  There is no interruption
of execution by an incoming message.  After the code in the matching
block (or timeout) is executed, the receive statement returns with the
value of the executed block, and execution continues at the statement
following the receive.

>     receive
>         {_, ComposerList, StartTime, XCoordinate, _, _, Distance,
> notdone} ->
>             some code I want executed
>             some code I want executed
>             some code I want executed;
>         {Base, _, StartTime, _, _, _, done}   ->
>             some code I want executed
>     end.
> =============================
> SO, will the three lines marked "some code I want executed" be
> executed always, or does a closely following message interrup it and
> send things back to the 'receive' (leaving some code perhaps not
> executed)?
> (...trying to find a problem...)

You might have to check if some of the code there is crashing the
process.  That will result in some code not being executed.  The closely
following message you talk of will be held in the process mailbox until
the process next calls receive.


More information about the erlang-questions mailing list