|
|
|
@ -121,7 +121,7 @@
|
|
|
|
|
|
|
|
|
|
这里发生错误是因为并没有 `ThreadPool` 上的 `execute` 方法。回忆 [“创建有限数量的线程”](#创建有限数量的线程) 部分我们决定线程池应该有与 `thread::spawn` 类似的接口,同时我们将实现 `execute` 函数来获取传递的闭包并将其传递给池中的空闲线程执行。
|
|
|
|
|
|
|
|
|
|
我们会在 `ThreadPool` 上定义 `execute` 函数来获取一个闭包参数。回忆第十三章的 [“将被捕获的值移出闭包和 `Fn` trait”][fn-traits] 部分,闭包作为参数时可以使用三个不同的 trait:`Fn`、`FnMut` 和 `FnOnce`。我们需要决定这里应该使用哪种闭包。最终需要实现的类似于标准库的 `thread::spawn`,所以我们可以观察 `thread::spawn` 的签名在其参数中使用了何种 bound。查看文档会发现:
|
|
|
|
|
我们会在 `ThreadPool` 上定义 `execute` 函数来获取一个闭包参数。回忆第十三章的 [“将捕获的值移出闭包和 `Fn` trait”][fn-traits] 部分,闭包作为参数时可以使用三个不同的 trait:`Fn`、`FnMut` 和 `FnOnce`。我们需要决定这里应该使用哪种闭包。最终需要实现的类似于标准库的 `thread::spawn`,所以我们可以观察 `thread::spawn` 的签名在其参数中使用了何种 bound。查看文档会发现:
|
|
|
|
|
|
|
|
|
|
```rust,ignore
|
|
|
|
|
pub fn spawn<F, T>(f: F) -> JoinHandle<T>
|
|
|
|
@ -395,6 +395,6 @@ Worker 2 got a job; executing.
|
|
|
|
|
[creating-type-synonyms-with-type-aliases]:
|
|
|
|
|
ch20-03-advanced-types.html#使用类型别名创建类型同义词
|
|
|
|
|
[integer-types]: ch03-02-data-types.html#整型
|
|
|
|
|
[fn-traits]: ch13-01-closures.html#将被捕获的值移出闭包和-fn-trait
|
|
|
|
|
[fn-traits]: ch13-01-closures.html#将捕获的值移出闭包和-fn-trait
|
|
|
|
|
[builder]: https://doc.rust-lang.org/std/thread/struct.Builder.html
|
|
|
|
|
[builder-spawn]: https://doc.rust-lang.org/std/thread/struct.Builder.html#method.spawn
|
|
|
|
|