<div dir="ltr">Try something like this...<br><br><span style="font-family: courier new,monospace;">-module(forkjoin).</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">-compile(export_all).</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">fork(FunList) when is_list(FunList) -></span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> process_flag(trap_exit, true),</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> ParentPid = self(),</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> [begin</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> WrappedFun =</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> fun () -></span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> ParentPid ! {'RESULT', self(), Fun()}</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> end,</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> spawn_link(WrappedFun)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> end ||</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> Fun <- FunList].</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">join(PidList) when is_list(PidList) -></span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> [receive</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> {'RESULT', Pid, Result} -></span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> {ok, Result};</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> {'EXIT', Pid, Reason} -></span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> {error, Reason}</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> end ||</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> Pid <- PidList].</span><br style="font-family: courier new,monospace;"><br><br><div class="gmail_quote">On Fri, Sep 19, 2008 at 11:43 AM, Gleb Peregud <span dir="ltr"><<a href="mailto:gleber.p@gmail.com">gleber.p@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">You may try to use erlang:exit/1 to make 'EXIT'/'DOWN' message<br>
"return" something instead of 'normal'.<br>
<br>
Take a look at plists: <a href="http://code.google.com/p/plists/" target="_blank">http://code.google.com/p/plists/</a> - it<br>
implements quite a lot of parallel list operations, which may be what<br>
you are looking for :)<br>
<div><div></div><div class="Wj3C7c"><br>
On Fri, Sep 19, 2008 at 4:06 PM, Zvi <<a href="mailto:exta7@walla.com">exta7@walla.com</a>> wrote:<br>
><br>
> Hi,<br>
><br>
> I trying to implement some Fork-Join constructs in Erlang using links or<br>
> monitors.<br>
> I do not understand why when process finished executing function normally,<br>
> it's (function's) return value isn't put into 'EXIT' or 'DOWN' messages?<br>
> I.e. instead of atom 'normal' return {normal, ResultOfFun} or something<br>
> similar.<br>
><br>
> {'EXIT',Pid,normal}<br>
><br>
> {'DOWN',Ref,process,Pid,normal}<br>
><br>
><br>
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<br>
> -module(fj).<br>
> -compile(export_all).<br>
><br>
> -define(N, erlang:system_info(schedulers) ).<br>
><br>
> test_monitor() -><br>
> % fork<br>
> PidRefs = [spawn_monitor(<br>
> fun() -> io:format("pid: ~w~n",[self()]) end) ||<br>
> _<-lists:seq(1,?N)],<br>
> io:format("PidRefs: ~w~n", [PidRefs]),<br>
> % join<br>
> [receive {'DOWN',Ref,process,Pid,_}->ok end || {Pid,Ref}<-PidRefs ].<br>
><br>
> test_link() -><br>
> % fork<br>
> process_flag(trap_exit, true),<br>
> Pids = [spawn_link(<br>
> fun() -> io:format("pid: ~w~n",[self()]) end) ||<br>
> _<-lists:seq(1,?N)],<br>
> io:format("Pids: ~w~n", [Pids]),<br>
> % join<br>
> [receive {'EXIT',Pid,_}->ok end || Pid<-Pids ].<br>
><br>
> P.S.<br>
> if there is some better ways to implement fork join please let me know.<br>
><br>
> Zvi<br>
> --<br>
> View this message in context: <a href="http://www.nabble.com/value-of-spawned-function-tp19573059p19573059.html" target="_blank">http://www.nabble.com/value-of-spawned-function-tp19573059p19573059.html</a><br>
> Sent from the Erlang Questions mailing list archive at Nabble.com.<br>
><br>
> _______________________________________________<br>
> erlang-questions mailing list<br>
> <a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
> <a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
><br>
<br>
<br>
<br>
</div></div><font color="#888888">--<br>
Gleb Peregud<br>
<a href="http://gleber.pl/" target="_blank">http://gleber.pl/</a><br>
<br>
Every minute is to be grasped.<br>
Time waits for nobody.<br>
-- Inscription on a Zen Gong<br>
</font><div><div></div><div class="Wj3C7c">_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
</div></div></blockquote></div><br></div>