Function std::thread::scoped [] [src]

pub fn scoped<'a, T, F>(f: F) -> JoinGuard<'a, T> where T: Send + 'a, F: FnOnce() -> T, F: Send + 'a
Deprecated since 1.2.0

: this unsafe API is unlikely to ever be stabilized in this form

Spawns a new scoped thread, returning a JoinGuard for it.

The spawn method does not allow the child and parent threads to share any stack data, since that is not safe in general. However, scoped makes it possible to share the parent's stack by forcing a join before any relevant stack frames are popped:

#![feature(scoped)] fn main() { use std::thread; let guard = thread::scoped(move || { // some work here }); // do some other work in the meantime let output = guard.join(); }
#![feature(scoped)]

use std::thread;

let guard = thread::scoped(move || {
    // some work here
});

// do some other work in the meantime
let output = guard.join();

The scoped function doesn't return a Thread directly; instead, it returns a join guard. The join guard can be used to explicitly join the child thread (via join), returning Result<T>, or it will implicitly join the child upon being dropped. Because the child thread may refer to data on the current thread's stack (hence the "scoped" name), it cannot be detached; it must be joined before the relevant stack frame is popped.

Panics

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