[erlang-questions] Join equivalent

Sean Hinde <>
Fri Sep 8 14:30:58 CEST 2006


> Is there an equivalent function in Erlang to “C pthreads join”?  
> This can semantically be implemented with linked process with  
> ‘receive to exit’, but what I want to know is whether there is a  
> direct BIF to call after spawn?
You are on the right track with "receive to exit". One nice trick you  
can do in erlang is to return the result of some short lived process  
in the exit message. This saves having to send a result message as  
well as handling the EXIT. Sketch of code here:

spawn(fun() -> short_lived() end),
receive
    {'EXIT', {ok, Result}} ->
       result_proc(Result);
   {'EXIT', Error} ->
	log_error(Error)
end.

short_lived() ->
	Res = do_work(),
         exit({ok, Res}).

Sean


>
>
> Thanks,
>
> N.Piriyatheepan
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20060908/acf6e72f/attachment.html>


More information about the erlang-questions mailing list