pub struct Scope<'scope, 'env> where
    'env: 'scope, 
{ data: ScopeData, scope: PhantomData<&'scope mut &'scope ()>, env: PhantomData<&'env mut &'env ()>, }
🔬 This is a nightly-only experimental API. (scoped_threads)
Expand description

A scope to spawn scoped threads in.

See scope for details.

Fields

data: ScopeData
🔬 This is a nightly-only experimental API. (scoped_threads)
scope: PhantomData<&'scope mut &'scope ()>
🔬 This is a nightly-only experimental API. (scoped_threads)

Invariance over ’scope, to make sure ’scope cannot shrink, which is necessary for soundness.

Without invariance, this would compile fine but be unsound:

#![feature(scoped_threads)]

std::thread::scope(|s| {
    s.spawn(|| {
        let a = String::from("abcd");
        s.spawn(|| println!("{a:?}")); // might run after `a` is dropped
    });
});
env: PhantomData<&'env mut &'env ()>
🔬 This is a nightly-only experimental API. (scoped_threads)

Implementations

🔬 This is a nightly-only experimental API. (scoped_threads)

Spawns a new thread within a scope, returning a ScopedJoinHandle for it.

Unlike non-scoped threads, threads spawned with this function may borrow non-'static data from the outside the scope. See scope for details.

The join handle provides a join method that can be used to join the spawned thread. If the spawned thread panics, join will return an Err containing the panic payload.

If the join handle is dropped, the spawned thread will implicitly joined at the end of the scope. In that case, if the spawned thread panics, scope will panic after all threads are joined.

This call will create a thread using default parameters of Builder. If you want to specify the stack size or the name of the thread, use Builder::spawn_scoped instead.

Panics

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

Trait Implementations

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.