Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The very next sentence after the one you quoted was, "Each actor manages its own private message queue." That covers everything you're saying, which is basically describing how actors are implemented.

The point is that it's not a shared message queue as in the COBOL case, or most enterprise queuing systems. An actor is a self-contained, isolated object with its own message handling and state, and it executes concurrently with all other actors.

As such, this is not quite correct:

> you must be aware that the input queue exists and it is not the same thing as the actor.

The term "actor" refers to the bundle of state, behavior (handler loop), and message queue.

You certainly must be aware of the existence of the queue though, but since you have to write a message handler loop for each actor, it would be difficult not to be.

 help



You can (of course) trivially implement a many to many queue from a many to one by using one actor to store-and-forward messages to several others.

Even if you don’t model the system this way explicitly in your head, this is what’s effectively happening if you have cascades of messages down a tree of actors.


I assume that in the COBOL case by "shared message queue" you mean "many-to-many" message queue.

I have written COBOL programs in the very distant past, but they were not concurrent programs, so I do not know how COBOL queues behave, so you are probably right.

But the input queues of actors must also be shared, i.e. they must be "many-to-one" message queues, because there are many other actors which can send messages to an actor.

There are 3 kinds of shared message queues: many-to-one, one-to-many and many-to-many. Whenever the message queues may be used by multiple threads, which is always on modern CPUs, the 3 kinds of shared message queues must use certain methods to allow concurrent accesses, which are different for the 3 kinds of queues.

Obviously, many-to-many queues have the most complex implementation, while many-to-one queues are simpler, but they must still use atomic instructions in the right way, to avoid data corruption. Only the one-to-one message queues do not need atomic instructions, but only store-release and load-acquire instructions.


By shared queue I mean that the queue doesn't belong to any single task or actor.

Each actor owns its own queue, and is the only reader. This makes it a many-to-one queue.

> Whenever the message queues may be used by multiple threads, which is always on modern CPUs, the 3 kinds of shared message queues must use certain methods to allow concurrent accesses, which are different for the 3 kinds of queues.

This is exactly what actor systems like Erlang avoid. The Erlang VM handles preemptive multitasking for the actors it manages, without using OS threads. This eliminates a lot of concurrency issues, and is a big part of why Erlang systems have such a reputation for reliability.

You can think of it like a green thread system where each thread has its own private message queue.

> but they must still use atomic instructions in the right way, to avoid data corruption.

This is only an issue because the message queues you're talking about are implemented separately from the OS, so there are no guarantees about concurrency behavior. This is exactly what the actor model solves for.


OK, with your definition for "shared queue" what you said initially was correct, but by "shared data structure" I understand that the access to it is shared between multiple active entities, even if the data structure is owned by only one of those entities, hence the miscommunication.

In general, in programs it does not matter much where you put artificial borders of ownership between entities, but whether the access to a resource is shared or exclusive has a crucial impact on the required code.

If threads are not visible to the programmer who writes the code for an actor, there are 2 cases. The first is when threads really do not exist, in which case the VM is single-threaded and executed on a single core of a modern CPU, so the achievable performance is modest, but there is no need for handling concurrency.

The second case is when the VM is in fact multi-threaded, which, as you say, must be the case for Erlang, but it manages the scheduling of the threads itself, and the allocation of actors to threads, in a manner transparent for the programmer.

In this case the VM must include code for managing concurrent accesses from multiple hardware threads to the message queues, so they still are shared-access queues, even if their implementation is provided by the VM.


Is there a reason why couldn't use both? External queues are useful in that I don't have to care about "who" the recipient is, just the job that I expect will be done by depositing something to a queue. "Who" becomes a consumer problem that the producer doesn't have to involve themselves with.

> I don't have to care about "who" the recipient is, just the job that I expect will be done by depositing something to a queue.

I mentioned that in my first comment. In actor systems, the idiomatic model is just to spawn a new actor for each job.

But if you want a multi-reader message queue, you can implement an actor that provides that, and there are standard implementations of this.

Actors are the primitive mechanism on which other concurrency mechanism can be built, without all the problems of traditional threads.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: