Fearless Concurrency · 18 min · free with an account

Channels

Lesson preview · about 30%

Do not communicate by sharing memory; share memory by communicating.

Core idea

The loop ends when the last sender is dropped.

Start with the idea

An mpsc channel moves values from many producers to one consumer, and the move is what makes it safe — once sent, the sender no longer owns the value. The receiver iterates until every sender is dropped, so the loop ends on its own rather than needing a sentinel message.

A channel separates the producer of a value from its consumer. In a multi-producer, single-consumer channel, several senders can submit messages to one receiver. Sending an owned String moves it into the channel; the sender cannot keep changing the same object afterward. That ownership transfer is a simple way to avoid sharing mutable state. If the sender needs its own copy too, make that duplication explicit.

The receiver loop can continue until every sender handle is dropped. A cloned sender counts as another live producer, even when it never sends anything.

Keep learning, for free

The rest of this lesson is waiting for you.

Create a free account to read the complete explanation, see the infographic, explore the code, and try the exercises. All 742 lessons and algorithm steps are included.

No payment details. Your account opens the complete Academy.

← All lessons in Concurrency & Async