Skip to content

Instantly share code, notes, and snippets.

@GeertVL-zz
Created July 4, 2012 19:44
Show Gist options
  • Save GeertVL-zz/3049213 to your computer and use it in GitHub Desktop.
Save GeertVL-zz/3049213 to your computer and use it in GitHub Desktop.
Erlang 8.11.2.2
-module(circle).
-export([start/1]).
start(Msg) ->
CPid = clasp(),
Pid1 = shackle(CPid, CPid),
Pid2 = shackle(Pid1, CPid),
Pid3 = shackle(Pid2, CPid),
send(Pid3,Msg).
clasp() ->
spawn(fun() ->
loop(0, self()) end).
shackle(Pid, Root) ->
spawn(fun() ->
loop(Pid, Root) end).
send(Pid,Msg) ->
Pid ! {next, Msg}.
loop(Pid, Root) ->
receive
{next, Msg} when is_pid(Pid) ->
io:format("Send request received ~p~n", [Pid]),
Pid ! {next, Msg},
loop(Pid, Root);
{next, Msg} when Pid == 0 ->
io:format("Arrived at shackle ~p~n", [Msg]),
Root ! {next, Msg};
{next} ->
io:format("Default way~n")
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment