Created
April 23, 2013 15:28
-
-
Save elvio/5444538 to your computer and use it in GitHub Desktop.
Erlang Ring example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(ring). | |
-export([build/1, ring_node/2, loop/1]). | |
build(N) -> | |
Manager = self(), | |
register(ring, Manager), | |
Root = spawn(?MODULE, ring_node, [null, Manager]), | |
Root ! {create, N}, | |
loop(N). | |
loop(N) -> | |
receive | |
ok -> | |
io:format("Ring was built~n"), | |
send_messages(N), | |
loop(N); | |
{message, finished} -> io:format("It finished~n"); | |
{message, Value} -> | |
loop(N) | |
end. | |
send_messages(0) -> | |
ring ! {message, finished}; | |
send_messages(N) -> | |
ring ! {message, N}, | |
send_messages(N-1). | |
ring_node(ChildPid, Manager) -> | |
receive | |
{create, 0} -> | |
Manager ! ok, | |
ring_node(ChildPid, Manager); | |
{create, N} -> | |
Child = spawn(?MODULE, ring_node, [null, Manager]), | |
Child ! {create, N-1}, | |
ring_node(Child, Manager) | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment