Created
March 31, 2014 06:33
-
-
Save mingshun/9886473 to your computer and use it in GitHub Desktop.
Exercise 1 of Chapter 12, Programming Erlang 2nd Edition
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
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