@@ -243,10 +243,43 @@ where
243
243
impl ThreadPoolBuilder {
244
244
/// Create a scoped `ThreadPool` initialized using this configuration.
245
245
///
246
- /// This is a convenience function for building a pool using a `crossbeam`
247
- /// scope to spawn threads in a [`spawn_handler`](#method.spawn_handler).
246
+ /// This is a convenience function for building a pool using [ `crossbeam::scope`]
247
+ /// to spawn threads in a [`spawn_handler`](#method.spawn_handler).
248
248
/// The threads in this pool will start by calling `wrapper`, which should
249
249
/// do initialization and continue by calling `ThreadBuilder::run()`.
250
+ ///
251
+ /// [`crossbeam::scope`]: https://docs.rs/crossbeam/0.7/crossbeam/fn.scope.html
252
+ ///
253
+ /// # Examples
254
+ ///
255
+ /// A scoped pool may be useful in combination with scoped thread-local variables.
256
+ ///
257
+ /// ```
258
+ /// #[macro_use]
259
+ /// extern crate scoped_tls;
260
+ /// # use rayon_core as rayon;
261
+ ///
262
+ /// scoped_thread_local!(static POOL_DATA: Vec<i32>);
263
+ ///
264
+ /// fn main() -> Result<(), rayon::ThreadPoolBuildError> {
265
+ /// let pool_data = vec![1, 2, 3];
266
+ ///
267
+ /// // We haven't assigned any TLS data yet.
268
+ /// assert!(!POOL_DATA.is_set());
269
+ ///
270
+ /// rayon::ThreadPoolBuilder::new()
271
+ /// .build_scoped(
272
+ /// // Borrow `pool_data` in TLS for each thread.
273
+ /// |thread| POOL_DATA.set(&pool_data, || thread.run()),
274
+ /// // Do some work that needs the TLS data.
275
+ /// |pool| pool.install(|| assert!(POOL_DATA.is_set())),
276
+ /// )?;
277
+ ///
278
+ /// // Once we've returned, `pool_data` is no longer borrowed.
279
+ /// drop(pool_data);
280
+ /// Ok(())
281
+ /// }
282
+ /// ```
250
283
pub fn build_scoped < W , F , R > ( self , wrapper : W , with_pool : F ) -> Result < R , ThreadPoolBuildError >
251
284
where
252
285
W : Fn ( ThreadBuilder ) + Sync , // expected to call `run()`
@@ -282,10 +315,56 @@ impl<S> ThreadPoolBuilder<S> {
282
315
///
283
316
/// Note that the threads will not exit until after the pool is dropped. It
284
317
/// is up to the caller to wait for thread termination if that is important
285
- /// for any invariants. For instance, threads created in `crossbeam::scope`
318
+ /// for any invariants. For instance, threads created in [ `crossbeam::scope`]
286
319
/// will be joined before that scope returns, and this will block indefinitely
287
320
/// if the pool is leaked. Furthermore, the global thread pool doesn't terminate
288
321
/// until the entire process exits!
322
+ ///
323
+ /// [`crossbeam::scope`]: https://docs.rs/crossbeam/0.7/crossbeam/fn.scope.html
324
+ ///
325
+ /// # Examples
326
+ ///
327
+ /// A minimal spawn handler just needs to call `run()` from an independent thread.
328
+ ///
329
+ /// ```
330
+ /// # use rayon_core as rayon;
331
+ /// fn main() -> Result<(), rayon::ThreadPoolBuildError> {
332
+ /// let pool = rayon::ThreadPoolBuilder::new()
333
+ /// .spawn_handler(|thread| {
334
+ /// std::thread::spawn(|| thread.run());
335
+ /// Ok(())
336
+ /// })
337
+ /// .build()?;
338
+ ///
339
+ /// pool.install(|| println!("Hello from my custom thread!"));
340
+ /// Ok(())
341
+ /// }
342
+ /// ```
343
+ ///
344
+ /// The default spawn handler sets the name and stack size if given, and propagates
345
+ /// any errors from the thread builder.
346
+ ///
347
+ /// ```
348
+ /// # use rayon_core as rayon;
349
+ /// fn main() -> Result<(), rayon::ThreadPoolBuildError> {
350
+ /// let pool = rayon::ThreadPoolBuilder::new()
351
+ /// .spawn_handler(|thread| {
352
+ /// let mut b = std::thread::Builder::new();
353
+ /// if let Some(name) = thread.name() {
354
+ /// b = b.name(name.to_owned());
355
+ /// }
356
+ /// if let Some(stack_size) = thread.stack_size() {
357
+ /// b = b.stack_size(stack_size);
358
+ /// }
359
+ /// b.spawn(|| thread.run())?;
360
+ /// Ok(())
361
+ /// })
362
+ /// .build()?;
363
+ ///
364
+ /// pool.install(|| println!("Hello from my fully custom thread!"));
365
+ /// Ok(())
366
+ /// }
367
+ /// ```
289
368
pub fn spawn_handler < F > ( self , spawn : F ) -> ThreadPoolBuilder < CustomSpawn < F > >
290
369
where
291
370
F : FnMut ( ThreadBuilder ) -> io:: Result < ( ) > ,
0 commit comments