87 lines
1.8 KiB
Rust
87 lines
1.8 KiB
Rust
use std::sync::mpsc;
|
|
use std::sync::Arc;
|
|
use std::sync::Mutex;
|
|
use std::thread;
|
|
|
|
enum Message {
|
|
NewJob(Job),
|
|
Terminate,
|
|
}
|
|
|
|
pub struct ThreadPool {
|
|
jobs: mpsc::Sender<Message>,
|
|
threads: Vec<Worker>,
|
|
}
|
|
|
|
trait FnBox {
|
|
fn call_box(self: Box<Self>);
|
|
}
|
|
|
|
impl<F: FnOnce()> FnBox for F {
|
|
fn call_box(self: Box<F>) {
|
|
(*self)()
|
|
}
|
|
}
|
|
|
|
type Job = Box<dyn FnBox + Send + 'static>;
|
|
|
|
impl ThreadPool {
|
|
pub fn new(thread_count: usize) -> Self {
|
|
let (jobs, receiver) = mpsc::channel();
|
|
let receiver = Arc::new(Mutex::new(receiver));
|
|
|
|
let mut threads: Vec<Worker> = Vec::new();
|
|
for id in 1..thread_count {
|
|
threads.push(Worker::new(id, Arc::clone(&receiver)));
|
|
}
|
|
|
|
ThreadPool { jobs, threads }
|
|
}
|
|
|
|
pub fn execute<F>(&self, f: F)
|
|
where
|
|
F: FnOnce() + Send + 'static,
|
|
{
|
|
let job = Box::new(f);
|
|
self.jobs.send(Message::NewJob(job)).unwrap();
|
|
}
|
|
}
|
|
|
|
impl Drop for ThreadPool {
|
|
fn drop(&mut self) {
|
|
for _ in &mut self.threads {
|
|
self.jobs.send(Message::Terminate).unwrap();
|
|
}
|
|
|
|
for worker in &mut self.threads {
|
|
if let Some(thread) = worker.thread.take() {
|
|
thread.join().unwrap();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
struct Worker {
|
|
_id: usize,
|
|
thread: Option<thread::JoinHandle<()>>,
|
|
}
|
|
|
|
impl Worker {
|
|
fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Message>>>) -> Worker {
|
|
let thread = thread::spawn(move || loop {
|
|
let message = receiver.lock().unwrap().recv().unwrap();
|
|
|
|
match message {
|
|
Message::NewJob(job) => job.call_box(),
|
|
Message::Terminate => {
|
|
break;
|
|
}
|
|
}
|
|
});
|
|
|
|
Worker {
|
|
_id: id,
|
|
thread: Some(thread),
|
|
}
|
|
}
|
|
}
|