[erlang-questions] Programming Erlang Exercise 8.11

Ladislav Lenart lenartlad@REDACTED
Thu Sep 20 09:47:46 CEST 2007


igwan wrote:
> Oops, forget it, I clicked on send too fast
> 
> The function I proposed will not fail when calling it, but exit at a 
> later time only when the second process happens to call register/2.
> I'm interested in a correct/atomic solution too :)
> 
> igwan
> 
> igwan a écrit :
>> start(AnAtom, Fun) ->
>>     Fun2 = fun() -> register(AnAtom, self()), Fun() end,
>>     spawn_link(Fun2).

Hello,

and what about this:

start(Atom, Fun) when is_atom(Atom), is_function(Fun, 0) ->
	Sender = self(),
	Fun2 = fun() ->
		case catch register(Atom, self()) of
			true ->
				Sender ! {started, self()},
				Fun();
			_ ->
				Sender ! {already_running, self()}
		end
	end,
	Pid = spawn(Fun2),
	receive
		{started, Pid} ->
			{ok, Pid};
		{already_running, Pid} ->
			already_running
	end.

Hope this helps,

Ladislav Lenart





More information about the erlang-questions mailing list