rust
51 lines · 9 steps
Building a thread pool in Rust
A fixed set of worker threads share one job channel, executing closures as they arrive and shutting down cleanly.
Explained by
highlit
1use std::sync::{mpsc, Arc, Mutex};
2use std::thread;
3use std::time::Duration;
4
5type Job = Box<dyn FnOnce() + Send + 'static>;
6
7pub struct WorkerPool {
8 workers: Vec<thread::JoinHandle<()>>,
9 sender: Option<mpsc::Sender<Job>>,
10}
11
12impl WorkerPool {
13 pub fn new(size: usize) -> Self {
14 let (sender, receiver) = mpsc::channel::<Job>();
15 let receiver = Arc::new(Mutex::new(receiver));
16 let mut workers = Vec::with_capacity(size);
17
18 for id in 0..size {
19 let receiver = Arc::clone(&receiver);
20 workers.push(thread::spawn(move || loop {
21 let job = match receiver.lock().unwrap().recv() {
22 Ok(job) => job,
23 Err(_) => break,
24 };
25 eprintln!("worker {id} picked up a job");
26 job();
27 }));
28 }
29
30 WorkerPool { workers, sender: Some(sender) }
31 }
32
33 pub fn submit<F>(&self, task: F)
34 where
35 F: FnOnce() + Send + 'static,
36 {
37 if let Some(sender) = &self.sender {
38 sender.send(Box::new(task)).expect("pool has shut down");
39 }
40 }
41}
42
43impl Drop for WorkerPool {
44 fn drop(&mut self) {
45 drop(self.sender.take());
46 for worker in self.workers.drain(..) {
47 let _ = worker.join();
48 }
49 thread::sleep(Duration::from_millis(1));
50 }
51}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Wrapping a channel receiver in Arc<Mutex<...>> lets many threads pull from one shared work queue safely.
- 2Boxing closures as a trait object turns arbitrary tasks into a single sendable job type.
- 3Dropping the sender closes the channel, which is the signal worker loops use to exit.
Related explainers
rust
use std::collections::HashMap; use std::sync::{Arc, Mutex}; use std::thread;
Aggregating metrics across threads in Rust
concurrency
shared-state
mutex
Intermediate
7 steps
java
public class ThumbnailProcessor { private static final int MAX_CONCURRENCY = 4;
Bounded parallel thumbnail rendering in Java
concurrency
thread-pool
futures
Intermediate
7 steps
go
package cache import ( "container/list"
Building a generic LRU cache in Go
lru-cache
generics
linked-list
Intermediate
8 steps
rust
use std::time::Instant; use tracing::info; pub struct Timer {
A scope-guard timer with Drop in Rust
raii
drop-guard
timing
Intermediate
7 steps
rust
use std::collections::HashMap; pub struct Memoizer<K, V, F> { cache: HashMap<K, V>,
A generic memoizer in Rust
memoization
generics
caching
Intermediate
6 steps
rust
use std::collections::HashMap; #[derive(Debug, Clone)] struct Order {
Aggregating Rust data with fold and entry
fold
hashmap
ownership
Intermediate
6 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/building-a-thread-pool-in-rust-explained-rust-dbe5/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.