[erlang-questions] concurrency question

Mark Bucciarelli mkbucc@REDACTED
Sat Oct 1 18:42:51 CEST 2016


On Fri, Sep 30, 2016 at 12:18 PM, Grzegorz Junka <list1@REDACTED> wrote:

>
> On 30/09/2016 01:25, Mark Bucciarelli wrote:
>
> I have a program that does the following:
>
> - start gen_event, naming it "dispatcher"
> - spawn a process
> - send 60 message to process then a stop message
> - exit
>
> ...

> But when I run it from inside the interpreter,
>
>     > simulation:start().
>
> I see:
>
> ** exception error: no such process or port
>
> (which is gen_event saying hey, I can't notify the Pid with that name
>
> because it's gone.)
>
>
> Why don't I get that error output when I run from the console?
>
>
> If you spawn the dispatcher from the shell then it will be linked to the
> shell process. So, won't be actually gone.
>
>

My question was not precise, but your answer was still correct I believe.

I was stopping the gen_event when the main loop ended.  But the child
process keeps running, and its call to gen_event:notify/2 fails.

So, if I understand correctly, inside the interpreter the child is linked
to the interpreter shell when the main loop stops.  Then, when the child
crashes it sends an exit signal to its one link, the shell.

Thanks, I have gained some understanding of how fast async messages and
mailbox buffers require you to think in a different way.

Here's the code of the main loop for reference.

-module(simulation).

-export([start/0]).

% The clock.
tictoc(60, Pid) ->
    Pid ! stop,
    ok;
tictoc(TimeInTics, Pid) ->
    Pid ! tic,
    tictoc(TimeInTics + 1, Pid).

% Spawn child process and start clock ticking.
run_clock() ->
    Pid = spawn_link(stoplight, start, [0, {30, 30, 6}]),
    tictoc(0, Pid).

% Simulation entry point.
start() ->
    {ok, _Pid} = gen_event:start({local, dispatcher}),
    ok = gen_event:add_handler(dispatcher, logger, []),
    run_clock(),
    gen_event:stop(dispatcher),
    io:put_chars("main routine is done.\n").

Mark

-- 
Blogging at markbucciarelli.com
Tweeting @mbucc
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20161001/7dc55442/attachment.htm>


More information about the erlang-questions mailing list