[erlang-questions] value of spawned function

Zvi <>
Fri Sep 19 16:06:39 CEST 2008


Hi,

I trying to implement some Fork-Join constructs in Erlang using links or
monitors.
I do not understand why when process finished executing function normally,
it's (function's) return value isn't put into 'EXIT' or 'DOWN' messages?
I.e. instead of atom 'normal' return {normal, ResultOfFun} or something
similar.

{'EXIT',Pid,normal}

{'DOWN',Ref,process,Pid,normal}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-module(fj).
-compile(export_all).

-define(N, erlang:system_info(schedulers) ).

test_monitor() ->
	% fork
	PidRefs = [spawn_monitor(
			fun() -> io:format("pid: ~w~n",[self()]) end) ||
		_<-lists:seq(1,?N)],
	io:format("PidRefs: ~w~n", [PidRefs]),
	% join
	[receive {'DOWN',Ref,process,Pid,_}->ok end || {Pid,Ref}<-PidRefs ].
	
test_link() ->
	% fork
	 process_flag(trap_exit, true),
	Pids = [spawn_link(
			fun() -> io:format("pid: ~w~n",[self()]) end) ||
		_<-lists:seq(1,?N)],
	io:format("Pids: ~w~n", [Pids]),
	% join
	[receive {'EXIT',Pid,_}->ok end || Pid<-Pids ].

P.S.
if there is some better ways to implement fork join please let me know.

Zvi
-- 
View this message in context: http://www.nabble.com/value-of-spawned-function-tp19573059p19573059.html
Sent from the Erlang Questions mailing list archive at Nabble.com.




More information about the erlang-questions mailing list