Вопрос

-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?

Это было полезно?

Решение

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).

Другие советы

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)),

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top