Skip to content

Commit

Permalink
Merge pull request #785 from laazy/patch-2
Browse files Browse the repository at this point in the history
Update ch20-03-graceful-shutdown-and-cleanup.md
  • Loading branch information
KaiserY committed Mar 21, 2024
2 parents bc39241 + a64e93e commit 34ba084
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/ch20-03-graceful-shutdown-and-cleanup.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
{{#include ../listings/ch20-web-server/listing-20-22/output.txt}}
```

这里的错误告诉我们并不能调用 `join`因为只有每一个 `worker` 的可变借用,而 `join` 获取其参数的所有权。为了解决这个问题,需要一个方法将 `thread` 移动出拥有其所有权的 `Worker` 实例以便 `join` 可以消费这个线程。示例 17-15 中我们曾见过这么做的方法:如果 `Worker` 存放的是 `Option<thread::JoinHandle<()>`,就可以在 `Option` 上调用 `take` 方法将值从 `Some` 成员中移动出来而对 `None` 成员不做处理。换句话说,正在运行的 `Worker``thread` 将是 `Some` 成员值,而当需要清理 worker 时,将 `Some` 替换为 `None`,这样 worker 就没有可以运行的线程了。
这里的错误告诉我们并不能调用 `join`因为我们只有每一个 `worker` 的可变借用,而 `join` 需要获取其参数的所有权。为了解决这个问题,需要一个方法将 `thread` 移动出拥有其所有权的 `Worker` 实例以便 `join` 可以消费这个线程。示例 17-15 中我们曾见过这么做的方法:如果 `Worker` 存放的是 `Option<thread::JoinHandle<()>`,就可以在 `Option` 上调用 `take` 方法将值从 `Some` 成员中移动出来而对 `None` 成员不做处理。换句话说,正在运行的 `Worker``thread` 将是 `Some` 成员值,而当需要清理 worker 时,将 `Some` 替换为 `None`,这样 worker 就没有可以运行的线程了。

为此需要更新 `Worker` 的定义为如下:

Expand Down

0 comments on commit 34ba084

Please sign in to comment.