Monday, March 10, 2008

Programming Erlang: Chapter 8 Problem 2

'Tis an odd title, I know.  My puppy is prancing around the kitchen trying to get me interested in his chew toy, dropping it near me so I can see that it bounces and wiggles.

Do you want to see Erlang bounce and wiggle? The code below creates a circular linked list of Erlang N processes, then sends a Message around it M times. Pretty cool, huh? Well, it was meant to be used by readers of "Programming Erlang" to compare their favorite language to Erlang. I didn't bother doing this with C# threads, though it would be interesting to try it out with the concurrency and co-ordination runtime.

Here's the out put of the code (time is in milliseconds):

Sent 30000000 messages in 27453 / 29186


-module(problem2).
-compile(export_all).

start(N, M, Message) when is_integer(N), is_integer(M) ->
statistics(runtime),
statistics(wall_clock),
FirstPid = spawn(?MODULE,loop,[[]]),
LastPid = create(1, N, FirstPid),
FirstPid ! LastPid, %% the ring is closed
send(0, M, FirstPid, Message),
{_, Time1} = statistics(runtime),
{_, Time2} = statistics(wall_clock),
io:format("Sent ~p messages in ~p / ~p~n", [N * M, Time1, Time2]).

send(C, C, _Pid, _Message) -> void;
send(I, C, Pid, Message) ->
Pid ! {self(), Message},
receive
finished -> void
end,
send(I+1, C, Pid, Message).

create(C, C, Pid) -> Pid;
create(I, C, Pid) ->
create(I+1, C, spawn(?MODULE, loop, [Pid])).

loop(Next) ->
receive
{Caller, FirstPid, Message} ->
if
FirstPid =:= self() -> Caller ! finished;
true -> Next ! {Caller, FirstPid, Message}
end,
loop(Next);
{Caller, Message} ->
Next ! {Caller, self(), Message},
loop(Next);
NewNext when Next =:= [] ->
loop(NewNext)
end.