|
|
|
|
|
|
|
|
pub struct ThreadPool; |
|
|
|
|
|
|
|
|
use std::thread; |
|
|
|
|
|
|
|
|
|
|
|
pub struct ThreadPool{ |
|
|
|
|
|
workers: Vec<Worker>, |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
impl ThreadPool { |
|
|
impl ThreadPool { |
|
|
pub fn new(size: usize) -> ThreadPool { |
|
|
pub fn new(size: usize) -> ThreadPool { |
|
|
assert!(size > 0); |
|
|
assert!(size > 0); |
|
|
ThreadPool |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut workers = Vec::with_capacity(size); |
|
|
|
|
|
|
|
|
|
|
|
for id in 0..size { |
|
|
|
|
|
workers.push(Worker::new(id)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ThreadPool { |
|
|
|
|
|
workers |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
pub fn execute<F>(&self, f: F) |
|
|
pub fn execute<F>(&self, f: F) |
|
|
where F: FnOnce() + Send + 'static { |
|
|
where F: FnOnce() + Send + 'static { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// pub fn spawn<F, T>(f: F) -> JoinHandle<T> |
|
|
|
|
|
// where |
|
|
|
|
|
// F: FnOnce() -> T + Send + 'static, |
|
|
|
|
|
// T: Send + 'static |
|
|
|
|
|
// { |
|
|
|
|
|
|
|
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
struct Worker { |
|
|
|
|
|
id: usize, |
|
|
|
|
|
thread: thread::JoinHandle<()>, |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl Worker { |
|
|
|
|
|
fn new(id: usize) -> Worker { |
|
|
|
|
|
let thread = thread::spawn(|| {}); |
|
|
|
|
|
|
|
|
|
|
|
Worker { |
|
|
|
|
|
id, |
|
|
|
|
|
thread, |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |