Question

-module(core_profile).

-export([start/0]).

start() ->
    register(eProfile, spawn(loop())),
    ok.

loop() ->
    receive
            {Key, Pid} -> Pid ! getKey();
            {name, Pid} -> Pid ! getName();
            {address, Pid} -> Pid ! getAddress()
    end,
    loop().

This is my code

when I try to start the loop from the shell i get stuck.

2> Pid = spawn(fun()-> a end).     
<0.39.0>
3>c(core_profile.erl).
{ok, core_profile}
4>core_profile:start().

shouldn't the command return ok and then give me the next command line, instead of doing nothing?

Was it helpful?

Solution

When you do spawn(loop()) the loop function is called to evaluate a result (which will be passed on to spawn, so you enter the infinite loop.

Instead of calling the function you must pass the function reference by doing spawn(fun loop/0).

OTHER TIPS

spawn(Fun) -> pid()

Types: Fun = function()

Returns the pid of a new process started by the application of Fun to the empty list [].

you should write register(eProfile, spawn( fun()-> loop() end)),

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top