views:

100

answers:

2

How does message loops in erlang work, are they sync when it comes to processing messages?

As far as I understand, the loop will start by "receive"ing a message and then perform something and hit another iteration of the loop.

So that has to be sync? right?

If multiple clients send messages to the same message loop, then all those messages are queued and performed one after another, or?

To process multiple messages in parallell, you would have to spawn multiple message loops in different processes, right?

Or did I misunderstand all of it?

+3  A: 

From the manual (Erlang concurrency

Each process has its own input queue for messages it receives. New messages received are put at the end of the queue. When a process executes a receive, the first message in the queue is matched against the first pattern in the receive, if this matches, the message is removed from the queue and the actions corresponding to the the pattern are executed.
However, if the first pattern does not match, the second pattern is tested, if this matches the message is removed from the queue and the actions corresponding to the second pattern are executed. If the second pattern does not match the third is tried and so on until there are no more pattern to test. If there are no more patterns to test, the first message is kept in the queue and we try the second message instead. If this matches any pattern, the appropriate actions are executed and the second message is removed from the queue (keeping the first message and any other messages in the queue). If the second message does not match we try the third message and so on until we reach the end of the queue. If we reach the end of the queue, the process blocks (stops execution) and waits until a new message is received and this procedure is repeated.
Of course the Erlang implementation is "clever" and minimizes the number of times each message is tested against the patterns in each receive.

So you could create prios with the regex, but the concurrency is done via multiple processes.

weismat
+6  A: 

Sending a message is asynchronous. Processing a message is synchronous - one message is receive'd at a time - because each process has its own (and only one) mailbox.

Frank Shearar

related questions