From 3a74de51debf4b41c24a385b9df2322ee8007a55 Mon Sep 17 00:00:00 2001 From: Unisko PENG Date: Thu, 6 Apr 2023 16:40:14 +0800 Subject: [PATCH] Update Ch20 --- hello/src/lib.rs | 10 +++++ ...ect_Building_a_Multithreaded_Web_Server.md | 41 +++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/hello/src/lib.rs b/hello/src/lib.rs index ce101c2..2699d40 100644 --- a/hello/src/lib.rs +++ b/hello/src/lib.rs @@ -62,3 +62,13 @@ impl Worker { Worker { id, thread } } } + +impl Drop for ThreadPool { + fn drop(&mut self) { + for worker in &mut self.workers { + println! ("关闭 worker {}", worker.id); + + worker.thread.join().unwrap(); + } + } +} diff --git a/src/Ch20_Final_Project_Building_a_Multithreaded_Web_Server.md b/src/Ch20_Final_Project_Building_a_Multithreaded_Web_Server.md index 993748e..9a7d7a8 100644 --- a/src/Ch20_Final_Project_Building_a_Multithreaded_Web_Server.md +++ b/src/Ch20_Final_Project_Building_a_Multithreaded_Web_Server.md @@ -1196,5 +1196,46 @@ impl Worker { **Implementing the `Drop` Trait on `ThreadPool`** +咱们来以在咱们的线程池上实现 `Drop` 开始。当线程池被丢弃时,咱们的那些线程就都应归拢,join 一下,以确保他们完成他们的工作。下面清单 20-22 给出了 `Drop` 实现的首次尝试;此代码尚不会很好地编译。 + +文件名:`src/lib.rs` + +```rust +impl Drop for ThreadPool { + fn drop(&mut self) { + for worker in &mut self.workers { + println! ("关闭 worker {}", worker.id); + + worker.thread.join().unwrap(); + } + } +} +``` + +*清单 20-22:在线程池超出作用域时归拢各个线程* + +> 注:关于线程的 `join` 方法,请参考 [Java Thread.join详解](https://zhuanlan.zhihu.com/p/57927767),[Joining Threads in Java](https://www.geeksforgeeks.org/joining-threads-in-java/)。 + +首选,咱们遍历了线程池中 `workers` 的各个线程。由于 `self` 是个可变引用,且咱们还需要能修改 `worker`,因此咱们为这个遍历使用了 `&mut`。对于各个 `worker`,咱们打印了讲到这个特定 `worker` 正要关闭的一条消息,并在随后在那个 `worker` 的线程上调用了 `join`。当到 `join` 的这个调用失败时,咱们便使用 `unwrap` 来令到 Rust 终止运行,并进入到非优雅有序关闭。 + +下面时在咱们编译这代码时,得到的报错信息: + +```console +$ cargo check + Checking hello v0.1.0 (/home/lenny.peng/rust-lang-zh_CN/hello) +error[E0507]: cannot move out of `worker.thread` which is behind a mutable reference + --> src/lib.rs:71:13 + | +71 | worker.thread.join().unwrap(); + | ^^^^^^^^^^^^^ ------ `worker.thread` moved due to this method call + | | + | move occurs because `worker.thread` has type `JoinHandle<()>`, which does not implement the `Copy` trait + | +note: `JoinHandle::::join` takes ownership of the receiver `self`, which moves `worker.thread` + --> /rustc/2c8cc343237b8f7d5a3c3703e3a87f2eb2c54a74/library/std/src/thread/mod.rs:1589:17 + +For more information about this error, try `rustc --explain E0507`. +error: could not compile `hello` due to previous error +```