Skip to content

Instantly share code, notes, and snippets.

@GeertVL-zz
Created July 5, 2012 13:03
Show Gist options
  • Save GeertVL-zz/3053553 to your computer and use it in GitHub Desktop.
Save GeertVL-zz/3053553 to your computer and use it in GitHub Desktop.
Erlang 8.11.2.3 first step (no counter and refactored)
-module(ring2).
-export([start/1]).
start(Msg) ->
Pid1 = ring(),
Pid2 = ring(),
Pid3 = ring(),
Pid4 = ring(),
shackle(Pid1, Pid2),
shackle(Pid2, Pid3),
shackle(Pid3, Pid4),
shackle(Pid4, Pid1),
send(Pid1, Msg).
shackle(LPid, RPid) ->
LPid ! {connect, LPid, RPid}.
ring() ->
spawn(fun() -> loop(0,0) end).
send(StartPid, Msg) ->
StartPid ! {send, Msg}.
loop(LPid, RPid) ->
receive
{connect, Left, Right} ->
io:format("shackle connected~n"),
loop(Left, Right);
{send, Msg} ->
io:format("Message sent: ~p~n", [RPid]),
RPid ! {send, Msg},
loop(LPid, RPid)
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment