Skip to content

Instantly share code, notes, and snippets.

@imbriaco
Created November 14, 2008 16:21
Show Gist options
  • Save imbriaco/24963 to your computer and use it in GitHub Desktop.
Save imbriaco/24963 to your computer and use it in GitHub Desktop.
Erlang (BEAM) emulator version 5.6.5 [source] [smp:2] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.6.5 (abort with ^G)
1> c("/Users/mark/Projects/erlang/excercises/parallel/ex3.erl", [{outdir, "/Users/mark/Projects/erlang/excercises/parallel"}]).
{ok,ex3}
2> ex3:start(foo, fun ex3:loop/0).
yes
3> ex3:start(foo, fun ex3:loop/0).
Function already registered.
ok
4> global:send(foo, { print, "whee!" }).
whee!
<0.37.0>
5> global:send(foo, { print, "whee!" }).
whee!
<0.37.0>
6> global:send(foo, exit).
<0.37.0>
7> ex3:start(foo, fun ex3:loop/0). yes
8> global:send(foo, { print, "alive again!" }).
alive again!
<0.43.0>
-module(ex3).
-compile(export_all).
loop() ->
receive
{print, Msg} ->
io:format("~s~n", [Msg]),
loop();
exit -> ok
end.
start(AnAtom, Fun) ->
global:trans({funLock, AnAtom}, fun() ->
case global:whereis_name(AnAtom) of
undefined ->
Pid = spawn(Fun),
global:register_name(AnAtom, Pid);
_ ->
io:format("Function already registered.~n")
end end).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment