[erlang-questions] gen_server and init
Jesper Louis Andersen
jesper.louis.andersen@REDACTED
Tue Jul 28 10:05:08 CEST 2015
On Mon, Jul 27, 2015 at 9:14 PM, Loïc Hoguin <essen@REDACTED> wrote:
> I'm not sure what you wrote there but I can give you two scenarios where
> it can fail off the top of my head. The first is very unlikely and can only
> fail if you use the 0 timeout, while the second is actually much easier to
> observe and can fail with both methods:
>
> P1 calls start_link
> P2 init (returns 0 timeout)
> P2 yields before calling receive
> P1 returns from start_link and sends P2 a message
> P2 receives message
>
>
Yes, this one is an obvious linearization problem. And indeed, using
concuerror for this is the right tool for the job. But if you send yourself
a message, concuerror reports, as expected that this is safe behaviour:
Done! (Exit status: completed)
Summary: 0 errors, 3/3 interleavings explored
and if that was not the case, there is something about the semantics of
Erlang I need to learn :)
> And:
>
> P1 calls start_link
> P2 in init subscribes to some kind of pubsub PS
> PS sends P2 message(s)
> P2 returns from init and receive those messages
>
The fact that you give other processes access to the Pid the guarantee is
lost. In that case, you can capture messages arriving before initialization
and do something sensible with them, or you can do the enter_loop dance you
suggested. I don't think it is a bad solution, but I would prefer to be
able to do something simpler in common cases, and indeed, sending yourself
a message is one of those, if we are to believe concuerror.
--
J.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150728/157032a2/attachment.htm>
More information about the erlang-questions
mailing list