[erlang-questions] Message send guarantees

Richard A. O'Keefe ok@REDACTED
Mon Jan 30 00:07:50 CET 2017

On 27/01/17 11:46 PM, Andrey Tsirulev wrote:
> 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?

Let's start by changing the destination from A to C.

A -> msg1 -> C
A -> msg2 -> B
B -> f(msg2) -> C

In this case, it is clearly NOT guaranteed that msg1 will arrive
before f(msg2).

Why would changing C to A make a difference?
Answer: because A -> A is happening in the same place.

Let E1 and E2 be two events.
E1 happens-before E2 if and only if
  (1) E1 and E2 happen in the same process
      E1 happens before E2 in that process's world-line
  (2) E1 is the transmission of a message
      and E2 is the receipt of the same message
  (3) There is an event Em such that
      E1 happens-before Em and Em happens-before E2

If we change the example to

A -> msg2 -> B
A -> msg1 -> A
B -> f(msg2) -> A

the transmission of msg1 now happens after the transmission
of msg2, so there's no reason to expect it to arrive earlier
than f(msg2).  Imagine a single-CPU implementation where
A -> msg2 -> B puts A to sleep and wakes B, then
B -> f(msg2) -> A puts B to sleep and wakes A,
then A -> msg1 -> A puts msg1 in A's queue after f(msg2).

More information about the erlang-questions mailing list