Completing the thought (a question about receive)

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Completing the thought (a question about receive)

Donald Steven
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:

     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...)

Thanks.

Don

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Completing the thought (a question about receive)

Kushal Kumaran
Hi Donald,

Donald Steven <[hidden email]> 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.

--
regards,
kushal
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Completing the thought (a question about receive)

Hugo Mills-2
In reply to this post by Donald Steven
On Mon, Feb 18, 2019 at 10:55:35AM -0500, Donald Steven wrote:
> 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.

   Messages are processed singly and sequentially by any one
process. Each process has a message queue, and the only time that a
message comes off that queue is when receive is executed. The receive
takes the first matching message off the queue and then runs the code
for the branch matched. Only when another receive is encountered will
the queue be looked at again.

   So, basically, there's no pre-emption of the code within a process
-- each process runs its code linearly and sequentially without
interruption.

   HTH,
   Hugo.

>  If I haven't expressed that clearly,
> consider:
>
>     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...)
>
> Thanks.
>
> Don
>
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions
--
Hugo Mills             | We demand rigidly defined areas of doubt and
hugo@... carfax.org.uk | uncertainty!
http://carfax.org.uk/  |                                           Vroomfondel
PGP: E2AB1DE4          |                  The Hitch-Hikers Guide to the Galaxy

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

signature.asc (853 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Completing the thought (a question about receive)

Donald Steven
Got it!  Thanks Hugo, much appreciated.

Don

On 2/18/2019 1.45 PM, Hugo Mills wrote:

> On Mon, Feb 18, 2019 at 10:55:35AM -0500, Donald Steven wrote:
>> 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.
>     Messages are processed singly and sequentially by any one
> process. Each process has a message queue, and the only time that a
> message comes off that queue is when receive is executed. The receive
> takes the first matching message off the queue and then runs the code
> for the branch matched. Only when another receive is encountered will
> the queue be looked at again.
>
>     So, basically, there's no pre-emption of the code within a process
> -- each process runs its code linearly and sequentially without
> interruption.
>
>     HTH,
>     Hugo.
>
>>    If I haven't expressed that clearly,
>> consider:
>>
>>      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...)
>>
>> Thanks.
>>
>> Don
>>
>> _______________________________________________
>> erlang-questions mailing list
>> [hidden email]
>> http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Completing the thought (a question about receive)

Fred Hebert-2
In reply to this post by Donald Steven
On 02/18, Donald Steven wrote:
>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)?
>

They will execute. A message is only handled when `receive' gets to be
able to grab one.
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Completing the thought (a question about receive)

Luke Bakken-2
In reply to this post by Donald Steven
Hi Donald,

The short answer is yes, the code you want executed is always run.
Messages don't preempt already running statements in a process.

I was looking through the docs to see where this is explained and this
does a decent job
-http://erlang.org/doc/getting_started/conc_prog.html

On Mon, Feb 18, 2019 at 7:55 AM Donald Steven <[hidden email]> wrote:

>
> 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:
>
>      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...)
>
> Thanks.
>
> Don
>
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Completing the thought (a question about receive)

Vance Shipley
In reply to this post by Hugo Mills-2
On Tue, Feb 19, 2019 at 1:05 PM Hugo Mills <[hidden email]> wrote:
> The receive takes the first matching message off the queue

Unless it's a "selective receive" in which case it may take some
earlier message off the queue.

--
     -Vance
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions