Skip to content

Commit

Permalink
Merge pull request #835 from wujunchengman/main
Browse files Browse the repository at this point in the history
更新表述
  • Loading branch information
KaiserY authored Dec 5, 2024
2 parents 417eab9 + 5500e2b commit cae285e
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/ch21-02-multithreaded.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ pub fn spawn<F, T>(f: F) -> JoinHandle<T>

我们将要实现的行为是创建线程并稍后发送代码,这会在 `ThreadPool` 和线程间引入一个新数据类型来管理这种新行为。这个数据结构称为 *Worker*,这是一个池实现中的常见概念。想象一下在餐馆厨房工作的员工:员工等待来自客户的订单,他们负责接受这些订单并完成它们。

不同于在线程池中储存一个 `JoinHandle<()>` 实例的 vector,我们会储存 `Worker` 结构体的实例。每一个 `Worker` 会储存一个单独的 `JoinHandle<()>` 实例。接着会在 `Worker` 上实现一个方法,它会获取需要允许代码的闭包并将其发送给已经运行的线程执行。我们还会赋予每一个 worker `id`,这样就可以在日志和调试中区别线程池中的不同 worker。
不同于在线程池中储存一个 `JoinHandle<()>` 实例的 vector,我们会储存 `Worker` 结构体的实例。每一个 `Worker` 会储存一个单独的 `JoinHandle<()>` 实例。接着会在 `Worker` 上实现一个方法,该方法将闭包发送到已经运行的线程中执行。我们还会赋予每一个 worker `id`,这样就可以在日志和调试中区别线程池中的不同 worker。

如下是创建 `ThreadPool` 时会发生的新过程。在通过如下方式设置完 `Worker` 之后,我们会实现向线程发送闭包的代码:

Expand Down

0 comments on commit cae285e

Please sign in to comment.