Hopper in use

Before we dig into the implementation of hopper, it'll be instructive to see it in practice. Let's adapt the last iteration of our ring program series. For reference, here's what that looked like:

use std::thread; use std::sync::mpsc; fn writer(chan: mpsc::SyncSender<u32>) -> () { let mut cur: u32 = 0; while let Ok(()) = chan.send(cur) { cur = cur.wrapping_add(1); } } fn reader(read_limit: usize, chan: mpsc::Receiver<u32>) -> () { let mut cur: u32 = 0; while (cur as usize) < read_limit { let num = chan.recv().unwrap(); assert_eq!(num, cur); cur = cur.wrapping_add(1); } } fn main() { let capacity = 10; let read_limit = 1_000_000; let (snd, rcv) = mpsc::sync_channel(capacity); let reader_jh = thread::spawn(move || { reader(read_limit, ...

Get Hands-On Concurrency with Rust now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.