expression

Notify a worker and join before reporting shutdown

A shutdown coordinator can notify a worker through a channel and join it before reporting completion. This finite fixture has no polling or timing assumptions. The worker must cooperate; joining a stuck worker can block indefinitely. Real services need bounded operations and a shutdown policy. Fixture: Expected stdout (not executed): worker stopped shutdown complete Expected stderr: (empty) Expected exit: 0 Common mistake: Reporting shutdown before workers finish or assuming dropping a JoinHandle stops its thread.

Formal Syntax & Structure

rust syntaxrust
stop.send(()); worker.join()

Working Example

Example Snippetrust
fn main() {    use std::{sync::mpsc, thread};    let (stop, inbox) = mpsc::channel::<()>();    let worker = thread::spawn(move || {        inbox.recv().unwrap();        "worker stopped"    });    stop.send(()).unwrap();    let report = worker.join().unwrap();    println!("{report}");    println!("shutdown complete");}
Rust Reference