Struct Scope

struct Scope<'env> { ... }

A scope for spawning threads.

Implementations

impl<'env> Scope<'env>

fn spawn<'scope, F, T>(self: &'scope Self, f: F) -> ScopedJoinHandle<'scope, T>
where
    F: FnOnce(&Scope<'env>) -> T + Send + 'env,
    T: Send + 'env

Spawns a scoped thread.

This method is similar to the spawn function in Rust's standard library. The difference is that this thread is scoped, meaning it's guaranteed to terminate before the scope exits, allowing it to reference variables outside the scope.

The scoped thread is passed a reference to this scope as an argument, which can be used for spawning nested threads.

The returned handle can be used to manually join the thread before the scope exits.

This will create a thread using default parameters of ScopedThreadBuilder, if you want to specify the stack size or the name of the thread, use this API instead.

Panics

Panics if the OS fails to create a thread; use ScopedThreadBuilder::spawn to recover from such errors.

Examples

use crossbeam_utils::thread;

thread::scope(|s| {
    let handle = s.spawn(|_| {
        println!("A child thread is running");
        42
    });

    // Join the thread and retrieve its result.
    let res = handle.join().unwrap();
    assert_eq!(res, 42);
}).unwrap();
fn builder<'scope>(self: &'scope Self) -> ScopedThreadBuilder<'scope, 'env>

Creates a builder that can configure a thread before spawning.

Examples

use crossbeam_utils::thread;

thread::scope(|s| {
    s.builder()
        .spawn(|_| println!("A child thread is running"))
        .unwrap();
}).unwrap();

impl Debug for Scope<'_>

fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result

impl Sync for Scope<'_>

impl<'env> Freeze for Scope<'env>

impl<'env> RefUnwindSafe for Scope<'env>

impl<'env> Send for Scope<'env>

impl<'env> Unpin for Scope<'env>

impl<'env> UnsafeUnpin for Scope<'env>

impl<'env> UnwindSafe for Scope<'env>

impl<T> Any for Scope<'env>

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for Scope<'env>

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for Scope<'env>

fn borrow_mut(self: &mut Self) -> &mut T

impl<T> From for Scope<'env>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T, U> Into for Scope<'env>

fn into(self: Self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom for Scope<'env>

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for Scope<'env>

fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>