Erlang - Spawn MFA vs Spawn Fun

I am trying to work with the Erlang Program, Version 2 (Joe Armstrong book). I am trying to solve the first problem in chapter 13.

As a solution to the problem - I came up with this -

-module(errorhandle1).
-export([my_spawn/3,loop/1,test_func/0]).

my_spawn(Mod,Fun,Args) -> 
   %SpawnedPidRef = myspawn_helper(Mod,Fun,Args),
   %spawn(?MODULE , loop , [myspawn_helper(Mod,Fun,Args)]).
   spawn(fun() -> loop(myspawn_helper(Mod,Fun,Args)) end).

myspawn_helper(Mod,Fun,Args) ->
   statistics(wall_clock),
   spawn_monitor(Mod,Fun,Args).

loop({SpPid,SpRef}) ->
   io:format("Created Pid is : ~p~n",[SpPid]),
   receive
      {makeError,Msg} -> 
          SpPid ! Msg,
          loop({SpPid,SpRef});
      {'DOWN',SpRef, process,SpPid,Why} ->
          {_, Time1} = statistics(wall_clock),
            io:format("Down"),
            io:format("Process spawn time = ~p microsecond ~n",[Time1])
end.

test_func() ->
    receive
        X -> 
           list_to_atom(X)
    end.

The above code works and creates the desired result (the first step is to solve the problem). Then I commented on the line and came up with the following program, which is exactly the same as the previous one, but I use the spawn / 3 function instead of spawn / 1, and I don't seem to get the desired result.

-module(errorhandle1).
-export([my_spawn/3,loop/1,test_func/0]).

my_spawn(Mod,Fun,Args) -> 
   %SpawnedPidRef = myspawn_helper(Mod,Fun,Args),
   spawn(?MODULE , loop , [myspawn_helper(Mod,Fun,Args)]).
   %spawn(fun() -> loop(myspawn_helper(Mod,Fun,Args)) end).

myspawn_helper(Mod,Fun,Args) ->
   statistics(wall_clock),
   spawn_monitor(Mod,Fun,Args).

loop({SpPid,SpRef}) ->
   io:format("Created Pid is : ~p~n",[SpPid]),
   receive
      {makeError,Msg} -> 
          SpPid ! Msg,
          loop({SpPid,SpRef});
      {'DOWN',SpRef, process,SpPid,Why} ->
          {_, Time1} = statistics(wall_clock),
            io:format("Down"),
            io:format("Process spawn time = ~p microsecond ~n",[Time1])
end.

test_func() ->
    receive
        X -> 
           list_to_atom(X)
    end.

Steps to complete the above module: with (errorhandle1). Pid = errorhandle1: my_spawn (errorhandle1, test_func, []). Pid! {MakeError, test}.

Can someone please help me with my understanding of using spawn / 3 and spawn / 1?

Thanks, Sathish.

+4
source share
2
spawn(fun() -> loop(myspawn_helper(Mod,Fun,Args)) end).

spawn(?MODULE , loop , [myspawn_helper(Mod,Fun,Args)]).

[myspawn_helper(Mod,Fun,Args)] spawn/3. . , myspawn_helper(Mod,Fun,Args) spawn/3 .

SpawnedPidRef = myspawn_helper(Mod,Fun,Args),
spawn(?MODULE , loop , [SpawnedPidRef]).

spawn/1

SpawnedPidRef = myspawn_helper(Mod,Fun,Args),
spawn(fun() -> loop(SpawnedPidRef) end).

. loop(SpawnedPidRef) . loop(myspawn_helper(Mod,Fun,Args)) .

spawn(fun() ->
        SpawnedPidRef = myspawn_helper(Mod,Fun,Args),
        loop(SpawnedPidRef)
    end).

. (. . - , - .)

+4

, , Pid! {MakeError, "" }. - , .

0

Source: https://habr.com/ru/post/1598258/


All Articles