Skip to content

Instantly share code, notes, and snippets.

@tecnowilliam
Created June 24, 2020 22:43
Show Gist options
  • Select an option

  • Save tecnowilliam/89478d508df80a6191dfecc6fb08c960 to your computer and use it in GitHub Desktop.

Select an option

Save tecnowilliam/89478d508df80a6191dfecc6fb08c960 to your computer and use it in GitHub Desktop.
% Compile: c(mailbox).
-module(mailbox).
-author("William Vargas").
-version("1.0").
-export([receiver/0, receiver_seq/0, tests/0]).
receiver() ->
timer:sleep(1000),
receive
stop ->
io:format("Stopped");
X ->
io:format("Message: ~w~n",[X]),
receiver()
end.
receiver_seq() ->
receive
{first, X} -> io:format("Received first message: ~w~n",[X])
end,
receive
{second, Y} -> io:format("Received second message: ~w~n",[Y])
end.
tests() ->
Rs = spawn(mailbox, receiver_seq, []),
Rs ! {first, 'FirstString'},
Rs ! {second, 'SecondString'},
R = spawn(mailbox, receiver, []),
R ! {ok, self()},
R ! stop.
@elbrujohalcon
Copy link

Good code! One caveat: 'FirstString' is not a string, it's an atom ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment