Skip to content

Instantly share code, notes, and snippets.

@arockwell
Created October 29, 2008 16:34
Show Gist options
  • Select an option

  • Save arockwell/20733 to your computer and use it in GitHub Desktop.

Select an option

Save arockwell/20733 to your computer and use it in GitHub Desktop.
-module(tut15).
-export([start/0, ping/2, pong/0]).
ping(0, Pong_PID) ->
Pong_PID ! finished,
io:format("ping finished~n", []);
ping(N, Pong_PID) ->
Pong_PID ! {ping, self()},
receive
pong ->
:format("Ping received pong~n", [])
end,
ping(N - 1, Pong_PID).
pong() ->
receive
finished ->
io:format("Pong finished~n", []);
{ping, Ping_PID} ->
io:format("Pong received ping~n", []),
Ping_PID ! pong,
pong()
end.
start() ->
Pong_PID = spawn(tut15, pong, []),
spawn(tut15, ping, [3, Pong_PID]).
------------
OUTPUT
>> start().
Pong received ping
Ping received pong
Pong received ping
Ping received pong
Pong received ping
Ping received pong
ping finished
Pong finished
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment