Erlang Language Processes Message Passing

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Two erlang processes can communicate with each other, wich is also known as message passing.
This procedure is asynchronous in the form that the sending process will not halt after sending the message.

Sending Messages

This can be achieved with the construct Pid ! Message, where Pid is a valid process identifier (pid) and Message is a value of any data type.

Each process has a "mailbox" that contains the received messages in the received order. This "mailbox" can be emptied with the build in flush/0 function.

If a message is send to a non existing process, the message will be discarded without any error!

An example might look like the following, where self/0 returns the pid of the current process and pid/3 creates a pid.

1> Pidsh = self().
<0.32.0>
2> Pidsh ! hello.
hello
3> flush().
Shell got hello
ok
4> <0.32.0> ! hello.
* 1: syntax error before: ’<’
5> Pidsh2 = pid(0,32,0).
<0.32.0>
6> Pidsh2 ! hello2.
hello2
7> flush().
Shell got hello2
ok

It is also possible to send a message to multiple processes at once, with Pid3!Pid2!Pid1!Msg.

Receiving Messages

Received messages can be processed with the receive construct.

receive
  Pattern1            -> exp11, .., exp1n1;
  Pattern2 when Guard -> exp21, .., exp2n2;
  ...
  Other               -> exp31, .., exp3n3;
  ...
  after Timeout       -> exp41, .., exp4n4
end

The Pattern will be compared to the messages in the "mailbox" starting with the first and oldest message.
If a pattern matches, the matched message is removed from the "mailbox" and the clause body is evaluated.

It is also possible to define timeouts with the after construct.
A Timeout is either the waiting time in milliseconds or the atom infinity.

The return value of receive is the last evaluated clause body.

Example (Counter)

A (very) simple counter with message passing might look like in the following.

-module(counter0).
-export([start/0,loop/1]).

% Creating the counter process.
start() ->
    spawn(counter0, loop, [0]).

% The counter loop.
loop(Val) ->
    receive
        increment ->
           loop(Val + 1)
    end.

Interaction with the counter.

1> C0 = counter0:start().
<0.39.0>
2> C0!increment.
increment
3> C0!increment.
increment


Got any Erlang Language Question?