Kernel.SpecialForms.receive
receive, go back to Kernel.SpecialForms module for more information.
Checks if there is a message matching the given clauses in the current process mailbox.
In case there is no such message, the current process hangs until a message arrives or waits until a given timeout value.
Examples
receive do
{:selector, number, name} when is_integer(number) ->
name
name when is_atom(name) ->
name
_ ->
IO.puts(:stderr, "Unexpected message received")
endAn optional after clause can be given in case the message was not
received after the given timeout period, specified in milliseconds:
receive do
{:selector, number, name} when is_integer(number) ->
name
name when is_atom(name) ->
name
_ ->
IO.puts(:stderr, "Unexpected message received")
after
5000 ->
IO.puts(:stderr, "No message in 5 seconds")
endThe after clause can be specified even if there are no match clauses.
The timeout value given to after can be any expression evaluating to
one of the allowed values:
:infinity- the process should wait indefinitely for a matching message, this is the same as not using the after clause0- if there is no matching message in the mailbox, the timeout will occur immediatelypositive integer smaller than or equal to
4_294_967_295(0xFFFFFFFFin hexadecimal notation) - it should be possible to represent the timeout value as an unsigned 32-bit integer.
Variable handling
The receive/1 special form handles variables exactly as the case/2
special macro. For more information, check the docs for case/2.