Skip to content

Instantly share code, notes, and snippets.

@ngerakines
Created August 26, 2008 18:16
Show Gist options
  • Save ngerakines/7311 to your computer and use it in GitHub Desktop.
Save ngerakines/7311 to your computer and use it in GitHub Desktop.
Eshell V5.6.2 (abort with ^G)
1> c(sleeperl).
{ok,sleeperl}
2> sleeperl:testb().
Receiver spawned at {1219,775147,552727}
Spawn start at {1219,775147,552864}
Spawn done at {1219,775147,553079}
ok
3>
receiver got '{done,<0.38.0>}' at {1219,775150,571689}
receiver got '{done,<0.40.0>}' at {1219,775150,573514}
receiver got '{done,<0.39.0>}' at {1219,775150,574316}
receiver got '{done,<0.41.0>}' at {1219,775150,583109}
receiver got '{done,<0.42.0>}' at {1219,775150,584700}
3>
-module(sleeperl).
-compile(export_all).
testa() ->
lists:map(fun(_) ->
os:cmd("/bin/sleep 0.1")
end, lists:seq(1, 50)).
testb() ->
io:format("Receiver spawned at ~p~n", [now()]),
Recv = spawn(fun() ->
thisloop([])
end),
io:format("Spawn start at ~p~n", [now()]),
lists:map(fun(_) ->
spawn(fun() ->
os:cmd("/bin/sleep 3"),
Recv ! {done, self()}
end)
end, lists:seq(1, 5)),
io:format("Spawn done at ~p~n", [now()]).
thisloop(Acc) ->
NewAcc = receive
X ->
io:format("receiver got '~p' at ~p~n", [X, now()]),
[X | Acc]
end,
thisloop(NewAcc).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment