Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are messages received by an actor unordered?

I've been studying the actor model (specifically the implementation in Scala) but I can't understand why there's a requirement that messages arrive in no particular order.

It seems like there are at least some elegant, actor-based solutions to concurrency problems that would work if only the messages arrived in order (e.g. producer-consumer variants, deferred database writes, concurrency-safe caches).

So why don't actor messages arrive in order? Is it to permit efficient implementation or maybe to prevent some kind of deadlock that would arise when messages are ordered?

like image 462
Bill Avatar asked Apr 22 '11 03:04

Bill


1 Answers

My impression is that if two threads send a message to an actor a, there is no particular guarantee about which will be received by the actor first. But if you have code that looks like

a ! "one"
a ! "two"

then a will always get "one" before "two" (though who knows what else might have arrived in between from other threads).

Thus, I don't think it is the case that messages arrive in no particular order at all. Multiple messages from within one thread will (as far as I can tell from the code or from experience) arrive in order.

like image 63
Rex Kerr Avatar answered Nov 14 '22 02:11

Rex Kerr