Update Ch20

This commit is contained in:
Unisko PENG 2023-04-04 18:07:51 +08:00
parent 2674e39d40
commit 66b7bb0d3e

View File

@ -2,7 +2,7 @@
use std::thread;
pub struct ThreadPool {
threads: Vec<thread::JoinHandle<()>>,
workers: Vec<Worker>,
}
impl ThreadPool {
@ -19,10 +19,10 @@ impl ThreadPool {
let mut threads = Vec::with_capacity(size);
for _ in 0..size {
// 创建出一些线程并将他们存储在那个矢量中
workers.push(Worker::new(id));
}
ThreadPool { threads }
ThreadPool { workers }
}
pub fn execute<F>(&self, f: F)
@ -31,3 +31,16 @@ impl ThreadPool {
{
}
}
struct Worker {
id: usize,
thread: thread::JoinHandle<()>,
}
impl Worker {
fn new(id: usize) -> Worker {
let thread = thread::spawn(|| {});
Worker { id, thread }
}
}