From 66b7bb0d3e3d2db9f7563d083630e7330a91c90b Mon Sep 17 00:00:00 2001 From: Unisko PENG Date: Tue, 4 Apr 2023 18:07:51 +0800 Subject: [PATCH] Update Ch20 --- hello/src/lib.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/hello/src/lib.rs b/hello/src/lib.rs index fbcd7d0..eb2d046 100644 --- a/hello/src/lib.rs +++ b/hello/src/lib.rs @@ -2,7 +2,7 @@ use std::thread; pub struct ThreadPool { - threads: Vec>, + workers: Vec, } 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(&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 } + } +}