19
19
//! # fn main() {
20
20
//!
21
21
//! // Create a worker thread pool with four threads
22
- //! let pool = CpuPool::new(4);
22
+ //! let pool = CpuPool::new(4).unwrap() ;
23
23
//!
24
24
//! // Execute some work on the thread pool, optionally closing over data.
25
25
//! let a = pool.spawn(long_running_future(2));
40
40
extern crate futures;
41
41
extern crate num_cpus;
42
42
43
+ use std:: io;
43
44
use std:: panic:: { self , AssertUnwindSafe } ;
44
45
use std:: sync:: { Arc , Mutex } ;
45
46
use std:: sync:: atomic:: { AtomicBool , AtomicUsize , Ordering } ;
@@ -147,16 +148,24 @@ impl CpuPool {
147
148
///
148
149
/// ```rust
149
150
/// # use futures_cpupool::{Builder, CpuPool};
151
+ /// # use std::io;
150
152
/// #
151
- /// # fn new(size: usize) -> CpuPool {
153
+ /// # fn new(size: usize) -> io::Result< CpuPool> {
152
154
/// Builder::new().pool_size(size).create()
153
155
/// # }
154
156
/// ```
155
157
///
158
+ /// # Errors
159
+ ///
160
+ /// This method yields an [`io::Result`] to capture any failure to
161
+ /// create the thread at the OS level.
162
+ ///
163
+ /// [`io::Result`]: https://doc.rust-lang.org/stable/std/io/type.Result.html
164
+ ///
156
165
/// # Panics
157
166
///
158
167
/// Panics if `size == 0`.
159
- pub fn new ( size : usize ) -> CpuPool {
168
+ pub fn new ( size : usize ) -> io :: Result < CpuPool > {
160
169
Builder :: new ( ) . pool_size ( size) . create ( )
161
170
}
162
171
@@ -167,12 +176,20 @@ impl CpuPool {
167
176
///
168
177
/// ```rust
169
178
/// # use futures_cpupool::{Builder, CpuPool};
179
+ /// # use std::io;
170
180
/// #
171
- /// # fn new_num_cpus() -> CpuPool {
181
+ /// # fn new_num_cpus() -> io::Result< CpuPool> {
172
182
/// Builder::new().create()
173
183
/// # }
174
184
/// ```
175
- pub fn new_num_cpus ( ) -> CpuPool {
185
+ ///
186
+ /// # Errors
187
+ ///
188
+ /// This method yields an [`io::Result`] to capture any failure to
189
+ /// create the thread at the OS level.
190
+ ///
191
+ /// [`io::Result`]: https://doc.rust-lang.org/stable/std/io/type.Result.html
192
+ pub fn new_num_cpus ( ) -> io:: Result < CpuPool > {
176
193
Builder :: new ( ) . create ( )
177
194
}
178
195
@@ -398,10 +415,17 @@ impl Builder {
398
415
399
416
/// Create CpuPool with configured parameters
400
417
///
418
+ /// # Errors
419
+ ///
420
+ /// This method yields an [`io::Result`] to capture any failure to
421
+ /// create the thread at the OS level.
422
+ ///
423
+ /// [`io::Result`]: https://doc.rust-lang.org/stable/std/io/type.Result.html
424
+ ///
401
425
/// # Panics
402
426
///
403
427
/// Panics if `pool_size == 0`.
404
- pub fn create ( & mut self ) -> CpuPool {
428
+ pub fn create ( & mut self ) -> io :: Result < CpuPool > {
405
429
let ( tx, rx) = mpsc:: channel ( ) ;
406
430
let pool = CpuPool {
407
431
inner : Arc :: new ( Inner {
@@ -424,9 +448,9 @@ impl Builder {
424
448
if self . stack_size > 0 {
425
449
thread_builder = thread_builder. stack_size ( self . stack_size ) ;
426
450
}
427
- thread_builder. spawn ( move || inner. work ( after_start, before_stop) ) . unwrap ( ) ;
451
+ thread_builder. spawn ( move || inner. work ( after_start, before_stop) ) ? ;
428
452
}
429
- return pool
453
+ Ok ( pool)
430
454
}
431
455
}
432
456
0 commit comments