Skip to content

Commit

Permalink
Merge #966
Browse files Browse the repository at this point in the history
966: Document an example with `std::thread::scope` r=cuviper a=cuviper

We only run tests on stable Rust, so this adds a doctest with the new `std::thread::scope`.
See also #948 to use that internally, but that will increase the crate MSRV.

Co-authored-by: Josh Stone <cuviper@gmail.com>
  • Loading branch information
bors[bot] and cuviper committed Aug 11, 2022
2 parents fea8141 + f79c44b commit c00b997
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions rayon-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ impl ThreadPoolBuilder {
/// The threads in this pool will start by calling `wrapper`, which should
/// do initialization and continue by calling `ThreadBuilder::run()`.
///
/// [`crossbeam::scope`]: https://docs.rs/crossbeam/0.7/crossbeam/fn.scope.html
/// [`crossbeam::scope`]: https://docs.rs/crossbeam/0.8/crossbeam/fn.scope.html
///
/// # Examples
///
Expand Down Expand Up @@ -340,7 +340,7 @@ impl<S> ThreadPoolBuilder<S> {
/// if the pool is leaked. Furthermore, the global thread pool doesn't terminate
/// until the entire process exits!
///
/// [`crossbeam::scope`]: https://docs.rs/crossbeam/0.7/crossbeam/fn.scope.html
/// [`crossbeam::scope`]: https://docs.rs/crossbeam/0.8/crossbeam/fn.scope.html
///
/// # Examples
///
Expand Down Expand Up @@ -385,6 +385,39 @@ impl<S> ThreadPoolBuilder<S> {
/// Ok(())
/// }
/// ```
///
/// This can also be used for a pool of scoped threads like [`crossbeam::scope`],
/// or [`std::thread::scope`] introduced in Rust 1.63, which is encapsulated in
/// [`build_scoped`](#method.build_scoped).
///
/// [`std::thread::scope`]: https://doc.rust-lang.org/std/thread/fn.scope.html
///
/// ```
/// # use rayon_core as rayon;
/// fn main() -> Result<(), rayon::ThreadPoolBuildError> {
/// std::thread::scope(|scope| {
/// let pool = rayon::ThreadPoolBuilder::new()
/// .spawn_handler(|thread| {
/// let mut builder = std::thread::Builder::new();
/// if let Some(name) = thread.name() {
/// builder = builder.name(name.to_string());
/// }
/// if let Some(size) = thread.stack_size() {
/// builder = builder.stack_size(size);
/// }
/// builder.spawn_scoped(scope, || {
/// // Add any scoped initialization here, then run!
/// thread.run()
/// })?;
/// Ok(())
/// })
/// .build()?;
///
/// pool.install(|| println!("Hello from my custom scoped thread!"));
/// Ok(())
/// })
/// }
/// ```
pub fn spawn_handler<F>(self, spawn: F) -> ThreadPoolBuilder<CustomSpawn<F>>
where
F: FnMut(ThreadBuilder) -> io::Result<()>,
Expand Down

0 comments on commit c00b997

Please sign in to comment.