How to Implement a Worker Pool in Rust

Implement a Rust worker pool using a ThreadPool struct, mpsc channels, and Arc<Mutex> to distribute closures to a fixed set of threads.

Implement a worker pool by defining a ThreadPool struct that manages a Vec<Worker> and uses an mpsc channel to distribute Job closures to threads.

use std::{sync::{Arc, Mutex, mpsc}, thread};

type Job = Box<dyn FnOnce() + Send + 'static>;

pub struct ThreadPool {
    workers: Vec<Worker>,
    sender: mpsc::Sender<Job>,
}

impl ThreadPool {
    pub fn new(size: usize) -> ThreadPool {
        assert!(size > 0);
        let (sender, receiver) = mpsc::channel();
        let receiver = Arc::new(Mutex::new(receiver));
        let mut workers = Vec::with_capacity(size);
        for id in 0..size {
            workers.push(Worker::new(id, Arc::clone(&receiver)));
        }
        ThreadPool { workers, sender }
    }

    pub fn execute<F>(&self, f: F)
    where
        F: FnOnce() + Send + 'static,
    {
        let job = Box::new(f);
        self.sender.send(job).unwrap();
    }
}

struct Worker {
    id: usize,
    thread: thread::JoinHandle<()>,
}

impl Worker {
    fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Job>>>) -> Worker {
        let thread = thread::spawn(move || loop {
            let message = receiver.lock().unwrap().recv();
            match message {
                Ok(job) => job(),
                Err(_) => break,
            }
        });
        Worker { id, thread }
    }
}
  1. Define the Job type alias as a boxed closure that is Send and lives for 'static.
  2. Create the ThreadPool struct holding a vector of Worker and an mpsc::Sender.
  3. Implement ThreadPool::new to create a channel, wrap the receiver in Arc<Mutex<>>, and spawn workers.
  4. Implement ThreadPool::execute to box the closure and send it through the sender.
  5. Define the Worker struct to hold an ID and a JoinHandle.
  6. Implement Worker::new to spawn a thread that loops, locks the receiver, and executes received jobs.