[erlang-questions] How exactly Erlang receive expression works?

Athanasios Valsamakis athanasios.valsamakis@REDACTED
Fri Mar 16 08:06:18 CET 2018


 I was reading the book of Mr. Joe Armstrong "Programming Erlang 2nd ed.",
when I reached the section about selective receive.
He explained it very clearly. So, based on his explanation I decided to
make a flowchart. I would like to share it with you. Comments are welcome.



      +-----------------+
      |  Enter receive. |               ** Timer may have already been
elapsed
      |  Start timer    |                when entering this state.
      +------+----------+
             |
       +-----v------+            +--------------------------------+
      +-------------------+
       | is mailbox | Yes        | suspend process until          |timer
elapsed | evaluate          |
+------> empty?     +------------> a new message is put in
mailbox+--------------> expressionsTimeout|
|      |            |            | or  timer has elapsed**        |
      |                   |
|      +------------+            +-----+--------------------------+
      +--------+----------+
|            |No                       |
              |
|     +------v----------------+        |new msg arrived
               |
|     | take the head message |        |
              |
|     | from the mailbox      <--------+
              |
|     +------+----------------+
               |
|            |
              |
|      +-----v-------------+    +---------------+    +-------------+
              |
|      | match the message +---->remove message +---->evaluate
 +------------+         |
|      | against pattern1  |Yes |from mailbox   |    |expressions1 |
    |         |
|      +-------------------+    +---------------+    +-------------+
    |         |
|            |No
    |         |
|            |
+-----v---------v--------------------+
|      +-----v-------------+    +---------------+    +-------------+      |
if any messages have been put into |
|      | match the message +---->remove message +---->evaluate     +------>
the save queue, are reentered      |
|      | against pattern2  |Yes |from mailbox   |    |expressions2 |      |
into the mailbox                   |
|      +-------------------+    +---------------+    +-------------+
+-----^---------+--------------------+
|            |No
    |         |
|            |
    |         |
|      +-----v-------------+    +---------------+    +-------------+
    |         |
|      | match the message +---->remove message +---->evaluate
 +------------+         |
|      | against pattern3  |Yes |from mailbox   |    |expressions3 |
              |
|      +-------------------+    +---------------+    +-------------+
              |
|            |No
              |
|            |
              |
|      +-----v------------------------------+
               |
|      | message is removed from the mailbox|
               |
+------+ and put into a "save queue"        |
               |
       |                                    |
               |
       +------------------------------------+
               |

              |
        +------------------+
              |
        | Exit receive.
<--------------------------------------------------------------+
        | Timer is cleared.|
        +------------------+
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20180316/9d22d4b9/attachment.htm>


More information about the erlang-questions mailing list