mirror of
https://github.com/darkicewolf50/RustBrock.git
synced 2025-06-15 04:54:17 -06:00
83 lines
2.0 KiB
Rust
83 lines
2.0 KiB
Rust
use std::{ sync::{ mpsc, Mutex, Arc }, thread };
|
|
|
|
pub struct ThreadPool {
|
|
workers: Vec<Worker>,
|
|
sender: Option<mpsc::Sender<Job>>,
|
|
}
|
|
|
|
impl ThreadPool {
|
|
/// Create a new ThreadPool.
|
|
///
|
|
/// The size is the number of threads in the pool.
|
|
///
|
|
/// # Panics
|
|
///
|
|
/// The `new` function will panic if the size is zero.
|
|
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: Some(sender) }
|
|
}
|
|
|
|
pub fn execute<F>(&self, f: F) where F: FnOnce() + Send + 'static {
|
|
let job = Box::new(f);
|
|
|
|
self.sender.as_ref().unwrap().send(job).unwrap();
|
|
}
|
|
|
|
// ambitious side project
|
|
// pub fn build(size: usize) -> Result<ThreadPool, PoolCreationError> {}
|
|
}
|
|
|
|
impl Drop for ThreadPool {
|
|
fn drop(&mut self) {
|
|
drop(self.sender.take());
|
|
|
|
for worker in &mut self.workers.drain(..) {
|
|
println!("Shutting down worker {}", worker.id);
|
|
|
|
worker.thread.join().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) => {
|
|
println!("Worker {id} got a job; executing");
|
|
job();
|
|
}
|
|
Err(_) => {
|
|
println!("Worker {id} disconnected; shutting down.");
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
Worker { id, thread }
|
|
}
|
|
}
|
|
|
|
type Job = Box<dyn FnOnce() + Send + 'static>;
|