Struct crossbeam_utils::thread::Scope
source · pub struct Scope<'env> {
handles: Arc<Mutex<Vec<Arc<Mutex<Option<JoinHandle<()>>>>>>>,
wait_group: WaitGroup,
_marker: PhantomData<&'env mut &'env ()>,
}
Expand description
A scope for spawning threads.
Fields§
§handles: Arc<Mutex<Vec<Arc<Mutex<Option<JoinHandle<()>>>>>>>
The list of the thread join handles.
wait_group: WaitGroup
Used to wait until all subscopes all dropped.
_marker: PhantomData<&'env mut &'env ()>
Borrows data with invariant lifetime 'env
.
Implementations§
source§impl<'env> Scope<'env>
impl<'env> Scope<'env>
sourcepub fn spawn<'scope, F, T>(&'scope self, f: F) -> ScopedJoinHandle<'scope, T>
pub fn spawn<'scope, F, T>(&'scope self, f: F) -> ScopedJoinHandle<'scope, T>
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();
sourcepub fn builder<'scope>(&'scope self) -> ScopedThreadBuilder<'scope, 'env>
pub fn builder<'scope>(&'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();