Skip to content

Message passing

Category: Communication · Status: stub · Lessons: chapter 05, Message passing (planned)

One line: Tasks share nothing and interact only by sending each other values, so that each value has one owner at a time.

How it connects

flowchart LR
  n_actor_model["Actor model"]
  n_channel["Channel"]
  n_csp["Communicating sequential processes"]
  n_message_passing["Message passing"]
  n_mpi["MPI"]
  n_publish_subscribe["Publish-subscribe and broadcast"]
  n_shared_memory["Shared memory"]
  n_actor_model -->|is a| n_message_passing
  n_channel -->|is a| n_message_passing
  n_csp -->|is a| n_message_passing
  n_message_passing ---|or| n_shared_memory
  n_mpi -->|uses| n_message_passing
  n_publish_subscribe -->|is a| n_message_passing
  classDef center stroke-width:3px
  class n_message_passing center
  classDef outside stroke-dasharray: 4 3
  class n_actor_model,n_channel,n_csp,n_mpi,n_publish_subscribe,n_shared_memory outside

In each language

Rust channels from std::sync::mpsc, multi-producer and single-consumer: Senders can be cloned, the Receiver cannot
Go goroutines and channels ↗; Effective Go ↗ puts it as share memory by communicating, instead of communicating by sharing memory
Java no channel type: threads hand objects over through a BlockingQueue, designed primarily for producer-consumer queues
Python queue.Queue between threads; between processes, multiprocessing pipes and queues, whose queues serialize every object put into them
C# System.Threading.Channels, for passing data between producers and consumers asynchronously
JavaScript a worker gets data through postMessage, which copies it with the structured clone algorithm or transfers it
Kotlin Channel, conceptually a BlockingQueue whose send and receive suspend instead of blocking
Erlang and Elixir the whole model: a process sends to another with ! (Erlang) or send/2 (Elixir), the message waits in the receiver's mailbox, and receive picks messages out by pattern (Erlang ↗, Elixir ↗)
Haskell Chan, an unbounded channel; an MVar can also act as a channel, with takeMVar and putMVar as receive and send
The operating system POSIX message queues ↗ let processes exchange data as messages, delivered highest priority first

Where to read more