Rust · Chapter 21 of 26
Channels
By the end of this chapter you can pass values from thread to thread with std::sync::mpsc channels: send and recv, many producers through cloned Senders, a receiver read as an iterator that ends when every Sender is gone, a bounded sync_channel that slows a fast producer to the pace of a slow consumer, and a pipeline of stages on their own threads. You can also say why a loop over a receiver never ended. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Concurrency and services
- 4 tasks
- Rust
- In the all labs plan
The first 2 chapters of Rust are free. This one comes with the all labs plan.
Why you need this
The plant's collector takes in what the 120 boxes send: on Tuesday, 10,164 pourings, and a society's totals must grow as they arrive, not after the last box has finished. In chapter 20 a thread could hand back a value only once, when it ended and someone joined it. A thread that sends 211 pourings one by one, while another adds them up, needs something else. Writing into one shared Vec from 120 threads is not allowed (two writers at once breaks chapter 6's rule), and the tools for sharing one value safely wait for chapter 22.
There is a second problem at the counter. The box reads cans from the analyser in milliseconds, and the village network uploads them far more slowly. A box that reads everything first and uploads later holds the whole shift in memory, and during a long network outage, the morning and the evening. The reader must be slowed down to the network's pace, without anyone writing a loop that checks and sleeps.
A channel is a queue between threads: one side sends values in, the other receives them out, in order. Values move through it, so no two threads ever own one at the same time.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
collector.rs: boxes send, one collector totals
Write collect(boxes: Vec<Vec<Collection>>) -> BTreeMap<String, u64>. Make one channel. For each box (a Vec of pourings) start a thread with thread::spawn that sends every one of its pourings into the channel. On the calling thread, receive every pouring and add its paise to its society's total, and return the totals by society.
For Tuesday's pourings, one box a society, it returns 120 totals, S001's 12415897 paise and every other society's own sum. With no boxes it returns an empty map. The Check gives each call at most 3 seconds, and looks for a channel and spawn in collector.rs.
- Task 2
queue.rs: a queue with room for 8
Write relay(reader: impl Iterator<Item = Collection> + Send + 'static, plant: &Plant) -> usize. Make a channel with room for 8 (mpsc::sync_channel(8)). Start a reader thread that takes each pouring from reader and sends it into the channel. On the calling thread, receive each pouring and upload it on its own, plant.upload(&society, &[pouring]) with the pouring's society. Join the reader thread, and return how many uploads succeeded.
The Check gives it 40 pourings and a plant that takes 5 ms an upload: it must take all 40, make 40 uploads and return 40, and while it runs the reader must get between 8 and 10 pourings ahead of the uploads at most (8 waiting in the queue, one in its hand); an unbounded channel lets it run 40 ahead. With S001's line down, S001's uploads fail and are not counted. With no pourings it returns 0. Each call has at most 3 seconds.
- Task 3
pipeline.rs: lines to cans to totals
Write pipeline(lines: Vec<String>) -> (usize, usize, u64) with stages on their own threads, joined by two channels: a reader thread sends each line; a parser thread receives the lines and sends, for each, the can's ml or a mark that it is broken; the calling thread receives those and returns (cans, broken lines, total ml of the cans).
A line is time,farmer,kind,litres,fat,snf. It is a can when it has exactly 6 fields and its litres, fat and SNF are all numbers; its ml is litres times 1000, rounded ("06:02:07,F10122,cow,4.88,4.2,8.6" is 4880 ml, and 1.13 litres is 1130). Anything else is broken. For S001's 211 analyser lines it returns (207, 4, 1935880). The Check also tries six lines (cans of 6.25 and 1.13 litres, an empty line, a line cut short, ERR for fat, and abc for litres), which give (2, 4, 7380), and no lines, (0, 0, 0). Each call has at most 3 seconds, and the Check looks for two channels and spawn in pipeline.rs.
- Task 4 · interview question
Interview: hang.rs, the loop that never ended
Lakhan's count_cans(boxes: Vec<Vec<Collection>>) -> usize starts a thread per box that sends each can into one channel, and counts what arrives with a for loop over the receiver. It counts every can and then never returns. Fix it, keeping the threads and the channel: for Tuesday's pourings, one box a society, it returns 10164, and with no boxes, 0. The Check gives each call at most 2 seconds.
Farhan will ask: why did the for loop over the receiver never end, when every box's thread had finished?
What you will learn
- mpsc::channel: a Sender and a Receiver; send and recv.
- Many producers: clone the Sender, one for each thread.
- The receiver as an iterator: a for loop that ends when every Sender is gone.
- The loop that never ends: a Sender still alive somewhere.
- mpsc::sync_channel(n): a channel with room for n, and back-pressure.
- Pipelines: stages on their own threads, joined by channels.
More in Concurrency and services
Build "Channels" yourself
DoodhSetu runs the milk collection boxes of 120 village societies around Anand. The old box program crashed on an empty line, and you rebuild it in Rust. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Rust are free. This one comes with the all labs plan.