Bounded background jobs
Quick answer
A bounded job queue limits pending work and forces the producer to handle overload. A worker's lifecycle must account for completion, cancellation and shutdown.
Admission and draining
A queue capacity controls queued items, not automatically memory per item or running worker count. Limit payload size and concurrency separately. A full queue can signal backpressure or reject new work. Closing the send side lets workers finish already accepted messages and exit after draining. A process-local channel is not a durable queue; crash recovery and redelivery need persistence and explicit acknowledgement policy.
use std::sync::mpsc::{sync_channel, TrySendError};fn main() { let (tx, rx) = sync_channel(1); tx.try_send(7).unwrap(); println!("{}", matches!(tx.try_send(8), Err(TrySendError::Full(8)))); println!("{}", rx.recv().unwrap());}Expected stdout (not executed):
true7The second item is returned in the Full error; it was not accepted.
use std::{sync::mpsc::sync_channel, thread};fn main() { let (tx, rx) = sync_channel::<u32>(2); let worker = thread::spawn(move || rx.into_iter().sum::<u32>()); tx.send(3).unwrap(); tx.send(4).unwrap(); drop(tx); println!("{}", worker.join().unwrap());}Expected stdout (not executed):
7Dropping the last sender allows iteration to end after all accepted items are received.
Common mistake
Do not wait for a worker that needs channel closure while still holding an unused sender clone.
Try it yourself
Extend the example to report rejected jobs and explain how a real durable queue would recover after a crash.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Bounded background jobs — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A queue holds at most 20 jobs, each with arbitrary payload size. Is total memory bounded by the queue count alone?
Bounded background jobs — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::sync::mpsc::{sync_channel, TrySendError}; fn main() { let (tx, rx) = sync_channel(1); tx.try_send(7).unwrap(); println!("{}", matches!(tx.try_send(8), Err(TrySendError::Full(8)))); println!("{}", rx.recv().unwrap()); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Bounded background jobs — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::{sync::mpsc::sync_channel, thread}; fn main() { let (tx, rx) = sync_channel::<u32>(2); let worker = thread::spawn(move || rx.into_iter().sum::<u32>()); tx.send(3).unwrap(); tx.send(4).unwrap(); drop(tx); println!("{}", worker.join().unwrap()); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.