std::thread

thread::spawn, JoinHandle::join, and thread::scope. Examples synchronize with join and do not use sleep or depend on scheduling order.

テーマページの本文は現在英語です。

std::thread::spawn starts a thread and returns a JoinHandle. join waits for that thread and returns Ok(value) or Err if the thread panicked. The examples wait on join before they print, so the output does not depend on which thread happened to finish first, and they do not call sleep.

spawn requires a Send + 'static closure. thread::scope, stable since Rust 1.63, lets closures borrow local data for the duration of the scope. The scope call does not return until its spawned threads have been joined or have otherwise finished.

Examples were compiled with rustc 1.90.0 using --edition=2024 and run as a plain binary. See the Rust 1.90.0 documentation for the complete API.

Rust reference overview →

API index

APISummary
thread::spawn, JoinHandle::join, thread::scopeStart a thread, wait for it with join, or borrow local data with scope. Printing happens after join, so the output is not a race with the scheduler.

spawn, join, and scope

The first thread returns 2 + 3; join yields 5. The second thread panics; join returns Err, and the example prints only is_err, not the panic payload. A silent panic hook is installed so the panic is still observed through join without writing hook text to stderr. Inside thread::scope, two threads borrow n and return n + 1 and n + 2. The prints use the join order (first, then second), not completion order.

thread::spawn, JoinHandle::join, thread::scope

pub fn spawn<F, T>(f: F) -> JoinHandle<T>
    where F: FnOnce() -> T + Send + 'static, T: Send + 'static
pub fn join(self) -> Result<T>
pub fn scope<'env, F, T>(f: F) -> T

Start a thread, wait for it with join, or borrow local data with scope. Printing happens after join, so the output is not a race with the scheduler.

ParameterDescription
fThe closure to run on the new thread. spawn requires Send + 'static. scope allows borrowing values that outlive the scope call.

Returns: spawn returns a JoinHandle. join returns Ok(T) if the thread finished, or Err with the panic payload if it panicked. scope returns the value of its closure after the scoped threads have finished.

Errors and panics

  • join Err — JoinHandle::join returns Err when the thread panicked. The example checks is_err and does not assert the payload display text.
  • Do not use sleep as a substitute for join. The examples were compiled with rustc 1.90.0 and --edition=2024.
rust
use std::thread;

fn main() {
    let handle = thread::spawn(|| 2 + 3);
    println!("{}", handle.join().unwrap());

    std::panic::set_hook(Box::new(|_| {}));
    let panicked = thread::spawn(|| panic!("boom")).join();
    println!("{}", panicked.is_err());

    let n = 10;
    thread::scope(|s| {
        let first = s.spawn(|| n + 1);
        let second = s.spawn(|| n + 2);
        println!("{} {}", first.join().unwrap(), second.join().unwrap());
    });
}

Expected output

5
true
11 12

Related: Arc and Mutex

Official documentation →