Skip to main content

spawn_thread

Function spawn_thread 

pub fn spawn_thread<T, F>(
    name: impl Into<String>,
    body: F,
) -> Result<JoinHandle<T>, Error>
where F: FnOnce() -> T + Send + 'static, T: Send + 'static,
Expand description

Start a named OS thread: the sanctioned replacement for std::thread::spawn and std::thread::Builder::spawn.

For work that owns its thread for a long time — an event loop, a device poller, a blocking protocol client. Short jobs belong on cpu(), the io pool, background, or a runtime’s blocking pool instead.

Unlike std::thread::spawn, this never panics: where Feature::Threads is unsupported (a browser page) it returns an error, and the caller degrades — skips optional work, or runs a short step inline.

§Errors

Returns the OS error when the thread cannot be created, and std::io::ErrorKind::Unsupported on a target without threads.