mirror of
https://github.com/gnu4cn/rust-lang-zh_CN.git
synced 2025-03-14 19:30:29 +08:00
Update Ch20
This commit is contained in:
parent
2674e39d40
commit
66b7bb0d3e
@ -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 }
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user