Before advent of threads as lightweight processing units that can share memory, processes had to share memory by communicating (IPC). To design concurrent processes that can scale, fault tolerant and efficiently distribute compute load, two approaches were proposed - CSP and Actor model.
In the 1978 paper Communicating Sequential Processes, Tony Hoare put forth a concurrency model that was designed around the fact that concurrent processes communicate among themselves for sharing messages and synchronization using channels
.
Go implements CSP using go routines and channels for example.
In the 1973 paper Synchronization in Actor Systems, Carl Hewitt came up with the Actor model to design communication among concurrent processes.
mailbox
which is associated with the other process it wants to communicate with to share memory.mailbox
is asynchronous, i.e, non blocking and sender and can continue with it’s own execution after message is queued in the mailbox
.mailbox
recieves a message, process is scheduled by a scheduler if not already running. After processing all messages in the mailbox
, it can be removed from the schedule again.Mailbox
or the communicating channel in the Actor model is anonymous and the processes have identities, each identified as an Actor
in the system.The Actor model can be found in one of it’s more popular implementation of Akka in Java.
Most modern languages have implemented this model of concurrency, where threads are compute units of a process that can share memory during execution without communication. To synchronize threads and prevent race conditions, mechanisms like locks, mutexes, signals and conditions are widely used.
Multithreading involves mutating shared memory among threads, which is very difficult to synchronize and detect design flaws. So unless the design mandates sharing of mutable memory between threads, it’s always preferable to go with CSP/Actor model. As mentioned in Effective Go:
Do not communicate by sharing memory; instead, share memory by communicating.
channels
was a synchronous operation. So compared to the Actor model where messages to mailboxes
are sent asynchronously, CSP doesn’t scale per request basis.channels
or the mode of communication is the primary object, processes are anonymous. In Actor model, mailboxes
are not created independently (associated with process) and have no existence outside scope of the Actor
process, which is the primary object here. This provides CSP flexibility to write to/read from multiple channels that can help design more intuitive systems unlike Actor model where processes are tied to mailboxes.