Skip to content

Instantly share code, notes, and snippets.

@mingshun
Created March 31, 2014 06:33
Show Gist options
  • Save mingshun/9886473 to your computer and use it in GitHub Desktop.
Save mingshun/9886473 to your computer and use it in GitHub Desktop.
Exercise 1 of Chapter 12, Programming Erlang 2nd Edition
start(AnAtom, Fun) when is_atom(AnAtom), is_function(Fun, 0) ->
Sender = self(),
Run = fun() ->
case catch register(AnAtom, self()) of
true ->
Sender ! {started, self()},
Fun();
_ ->
Sender ! {already_running, self()}
end
end,
Pid = spawn(Run),
receive
{started, Pid} ->
{ok, Pid};
{already_running, Pid} ->
already_running
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment