trpl-zh-cn/src/ch20-04-storing-threads.md

39 lines
1.7 KiB
Markdown
Raw Normal View History

2017-08-09 18:31:09 +08:00
## 创建线程池并储存线程
> [ch20-04-storing-threads.md](https://github.com/rust-lang/book/blob/master/second-edition/src/ch20-04-storing-threads.md)
> <br>
> commit d06a6a181fd61704cbf7feb55bc61d518c6469f9
之前的警告是因为在 `new``execute` 中没有对参数做任何操作。让我们用期望的实际行为实现他们。
### 验证池中的线程数
以考虑 `new` 作为开始。之前提到使用无符号类型作为 `size` 参数的类型,因为为负的线程数没有意义。然而,零个线程同样没有意义,不过零是一个完全有效的 `u32` 值。让我们在返回 `ThreadPool` 之前检查 `size` 是否大于零,并使用 `assert!` 宏在得到零时 panic如列表 20-13 所示:
<span class="filename">文件名: src/lib.rs</span>
```rust
# pub struct ThreadPool;
impl ThreadPool {
/// Create a new ThreadPool.
///
/// The size is the number of threads in the pool.
///
/// # Panics
///
/// The `new` function will panic if the size is zero.
pub fn new(size: u32) -> ThreadPool {
assert!(size > 0);
ThreadPool
}
// ...snip...
}
```
<span class="caption">列表 20-13实现 `ThreadPool::new``size` 为零时 panic</span>
趁着这个机会我们用文档注释为 `ThreadPool` 增加了一些文档。注意这里遵循了良好的文档实践并增加了一个部分提示函数会 panic 的情况,正如第十四章所讨论的。尝试运行 `cargo doc --open` 并点击 `ThreadPool` 结构体来查看生成的 `new` 的文档看起来如何!
相比像这里使用 `assert!` 宏,也可以让 `new` 像之前 I/O 项目中列表 12-9 中 `Config::new` 那样返回一个 `Result`