[erlang-questions] spawn and controlling_process

Hynek Vychodil vychodil.hynek@REDACTED
Mon Nov 3 10:06:28 CET 2014


No, there is not any difference. Both versions of code is compiled exactly
same:

fun1(Socket,Mod, Fun, Args) ->
    Pid = erlang:spawn(Mod, Fun, Args),
    gen_tcp:controlling_process(Socket,Pid).

fun2(Socket,Mod, Fun, Args) ->
    gen_tcp:controlling_process(Socket,erlang:spawn(Mod, Fun, Args)).

Then you can compile it with 'S' and get "assembler":

{function, fun1, 4, 2}.
  {label,1}.
    {line,[{location,"test2.erl",5}]}.
    {func_info,{atom,test2},{atom,fun1},4}.
  {label,2}.
    {allocate,1,4}.
    {move,{x,0},{y,0}}.
    {move,{x,1},{x,0}}.
    {move,{x,2},{x,1}}.
    {move,{x,3},{x,2}}.
    {line,[{location,"test2.erl",6}]}.
    {call_ext,3,{extfunc,erlang,spawn,3}}.
    {move,{x,0},{x,1}}.
    {move,{y,0},{x,0}}.
    {line,[{location,"test2.erl",7}]}.
    {call_ext_last,2,{extfunc,gen_tcp,controlling_process,2},1}.


{function, fun2, 4, 4}.
  {label,3}.
    {line,[{location,"test2.erl",9}]}.
    {func_info,{atom,test2},{atom,fun2},4}.
  {label,4}.
    {allocate,1,4}.
    {move,{x,0},{y,0}}.
    {move,{x,1},{x,0}}.
    {move,{x,2},{x,1}}.
    {move,{x,3},{x,2}}.
    {line,[{location,"test2.erl",10}]}.
    {call_ext,3,{extfunc,erlang,spawn,3}}.
    {move,{x,0},{x,1}}.
    {move,{y,0},{x,0}}.
    {line,[{location,"test2.erl",10}]}.
    {call_ext_last,2,{extfunc,gen_tcp,controlling_process,2},1}.

As you can see, both functions are exactly same. Why should be there any
difference.

Hynek Vychodil

On Sat, Nov 1, 2014 at 11:34 AM, Gadi Srebnik <gadi@REDACTED> wrote:

> Hi all, my first question here.
>
> How safe it is to use controlling_process after spawn? I am using
> additional gen_tcp:controlling_process inside the Mod:Fun itself after few
> additional commands. Is it possible that process will not be owner by then?
>
>
> Pid = erlang:spawn(Mod, Fun, Args),
>
>  gen_tcp:controlling_process(Socket,Pid),
>
> Is one-liner safer?
>
> gen_tcp:controlling_process(Socket,erlang:spawn(Mod, Fun, Args)),
>
>
> Thanks,
> Gadi
>
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20141103/260ac0e5/attachment.htm>


More information about the erlang-questions mailing list