[erlang-questions] Message send guarantees

Andrey Tsirulev andrey@REDACTED
Fri Jan 27 11:46:53 CET 2017


26.01.2017 10:14, Raimo Niskanen wrote:
> On Wed, Jan 25, 2017 at 11:06:05PM +0000, Dmitry Kakurin wrote:
>> No you cannot assume that. The message is considered "in transit" until the receiving process inspects it somehow.
>> What aspect of implementation can cause such reordering? Could you give an example how this could happen? Maybe I'll be able to artificially create conditions where it would not happen. Or work around it in some other way. Please see below why.
> On a multi-threade system it is very hard to know in which order things
> happen, so there is no "reordering" since there is no temporal order
> to begin with.
What about the following example?

A -- msg1 ---> A
A -- msg2 ---> B
B -- msg2 ---> A

In this case, is it guaranteed that if both msg2 and msg1 get to A's msg 
queue, msg1 gets there earlier?

If this is guaranteed, what makes this example *really* different from 
the original example?

I use this pattern for delayed gen_server's initialization if it's time 
consuming, like that:
init(Args) ->
     self() ! {init, Args},
handle_info({init, Args}, State) ->
   %% time consuming initialization here

to return from init ASAP so that start/start_link do not block for a 
long time. Then I expect that after

P = gen_server:start_link(...),
P ! test

when test is received, gen_server is fully initialized.  It never caused 
problems, but it might be luck.

Best Regards,

More information about the erlang-questions mailing list