In what order does the Erlang process use messages?

Are messages processed in the order they were received for the first time, or are they sorted by timestamp or something like that?

+7
source share
3 answers

The message order is maintained between the process and the other. Reading from the FAQ :

10.9 Is message reception guaranteed?

Yes, but only during one process.

If there is a living process, and you send him message A, and then message B, he guaranteed that when message B arrived, message A arrived earlier.

On the other hand, imagine processes P, Q, and R. P sends message A to Q, and then message B to R. There is no guarantee that A arrives at B. (It would be quite difficult for Distributed Erlan if that were required!)

@knutin is right about how you can consume messages in the process. Note that you can use the two following receive statements to ensure that a particular message will be consumed after another:

receive first -> do_first() end, receive second -> do_second() end 

The receive statement blocks. This ensures that you never do_second() before you do_first() . The difference from the second solution of @knutin is that in this case, if something unimportant comes immediately in front of the important one, you put in the queue an important bit.

+12
source

The mailbox is always stored in the order in which messages are received.

However, the order in which messages are used is determined by your code.

If you have a simple process with a general receive clause that receives something, the order in which you receive messages is the same as the order in which they were received.

 loop() -> receive Any -> do_something(Any), loop() end. 

However, if you have a selective receive with sentences with a word, it will look for a mailbox for messages of this particular type and consume the first relevant message, effectively skipping inappropriate messages. In the following example, if there are messages marked as important in the queue, they will be processed before any other message. Note. Matching this way will search for all messages in the queue, which is a problem for many messages. There have been some changes in this area, but I'm not ready for speed.

 loop() -> receive {important, Stuff} -> do_something_important(Stuff), loop(); Any -> do_something(Any) loop() end. 
+4
source

to further define the answer, I would like to point out the fact that, as indicated above, messages that do not match the template are skipped, but in fact they are simply put off, and then put back in order (so another message appeared first after inappropriate messages) for the next matching pattern.

This problem actually shows its worst if you have, for example, the gen_server behavior module, because in this case, having always the same pattern matching scheme, messages that are not in scope are going to send a message queue if you don't define (ugly and buggy, IMHO) match-all pattern like:

 receive ... -> ...; ... -> ...; MatchAllPatterns -> ok. end 
+1
source

All Articles