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
- Kinds: Actor model, Channel, Communicating sequential processes, Publish-subscribe and broadcast
- Is used by: MPI
- An alternative to: Shared memory
- See also: Inter-process communication, Thread confinement
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¶
- In this library: Keeping every update
- In the books: Learn Concurrent Programming with Go, James Cutajar — ch. 7, 'Communication using message passing'
- In the books: Modern Multithreading, Richard H. Carver, Kuo-Chung Tai — ch. 5, 'Message Passing'
- In the books: Concurrent Programming on Windows, Joe Duffy — ch. 13, 'Data and Task Parallelism' → 'Message-Based Parallelism'
- In the books: Multithreaded JavaScript, Thomas Hunter II, Bryan English — ch. 2, 'Browsers' → 'Message Passing Abstractions'
- In the books: Distributed Graph Algorithms for Computer Networks, K. Erciyes — ch. 3, 'The Computational Model' → 'Message Passing'
- In the books: The Rust Programming Language, Steve Klabnik, Carol Nichols — ch. 16, 'Fearless Concurrency' → 'Using Message Passing to Transfer Data Between Threads'
- Notes: Broker - sender - receiver - in general ↗
- Reference: Wikipedia: Message passing ↗