Module std::thread [] [src]

Native threads

The threading model

An executing Rust program consists of a collection of native OS threads, each with their own stack and local state.

Communication between threads can be done through channels, Rust's message-passing types, along with other forms of thread synchronization and shared-memory data structures. In particular, types that are guaranteed to be threadsafe are easily shared between threads using the atomically-reference-counted container, Arc.

Fatal logic errors in Rust cause thread panic, during which a thread will unwind the stack, running destructors and freeing owned resources. Thread panic is unrecoverable from within the panicking thread (i.e. there is no 'try/catch' in Rust), but the panic may optionally be detected from a different thread. If the main thread panics, the application will exit with a non-zero exit code.

When the main thread of a Rust program terminates, the entire program shuts down, even if other threads are still running. However, this module provides convenient facilities for automatically waiting for the termination of a child thread (i.e., join).

The Thread type

Threads are represented via the Thread type, which you can get in one of two ways:

Threads can be named, and provide some built-in support for low-level synchronization (described below).

The thread::current() function is available even for threads not spawned by the APIs of this module.

Spawning a thread

A new thread can be spawned using the thread::spawn function:

fn main() { use std::thread; thread::spawn(move || { // some work here }); }
use std::thread;

thread::spawn(move || {
    // some work here
});

In this example, the spawned thread is "detached" from the current thread. This means that it can outlive its parent (the thread that spawned it), unless this parent is the main thread.

The parent thread can also wait on the completion of the child thread; a call to spawn produces a JoinHandle, which provides a join method for waiting:

fn main() { use std::thread; let child = thread::spawn(move || { // some work here }); // some work here let res = child.join(); }
use std::thread;

let child = thread::spawn(move || {
    // some work here
});
// some work here
let res = child.join();

The join method returns a Result containing Ok of the final value produced by the child thread, or Err of the value given to a call to panic! if the child panicked.

Configuring threads

A new thread can be configured before it is spawned via the Builder type, which currently allows you to set the name and stack size for the child thread:

fn main() { #![allow(unused_must_use)] use std::thread; thread::Builder::new().name("child1".to_string()).spawn(move || { println!("Hello, world!"); }); }
use std::thread;

thread::Builder::new().name("child1".to_string()).spawn(move || {
    println!("Hello, world!");
});

Blocking support: park and unpark

Every thread is equipped with some basic low-level blocking support, via the park and unpark functions.

Conceptually, each Thread handle has an associated token, which is initially not present:

In other words, each Thread acts a bit like a semaphore with initial count 0, except that the semaphore is saturating (the count cannot go above 1), and can return spuriously.

The API is typically used by acquiring a handle to the current thread, placing that handle in a shared data structure so that other threads can find it, and then parking. When some desired condition is met, another thread calls unpark on the handle.

The motivation for this design is twofold:

Thread-local storage

This module also provides an implementation of thread local storage for Rust programs. Thread local storage is a method of storing data into a global variable which each thread in the program will have its own copy of. Threads do not share this data, so accesses do not need to be synchronized.

At a high level, this module provides two variants of storage:

Both forms of thread local storage provide an accessor function, with, which will yield a shared reference to the value to the specified closure. Thread-local keys only allow shared access to values as there is no way to guarantee uniqueness if a mutable borrow was allowed. Most values will want to make use of some form of interior mutability through the Cell or RefCell types.

Structs

Builder

Thread configuration. Provides detailed control over the properties and behavior of new threads.

JoinHandle

An owned permission to join on a thread (block on its termination).

LocalKey

A thread local storage key which owns its contents.

Thread

A handle to a thread.

JoinGuard [Unstable]

An RAII-style guard that will block until thread termination when dropped.

ScopedKey [Unstable]

Type representing a thread local storage key corresponding to a reference to the type parameter T.

Enums

LocalKeyState [Unstable]

Indicator of the state of a thread local storage key.

Functions

current

Gets a handle to the thread that invokes it.

panicking

Determines whether the current thread is unwinding because of panic.

park

Blocks unless or until the current thread's token is made available.

park_timeout_ms

Blocks unless or until the current thread's token is made available or the specified duration has been reached (may wake spuriously).

sleep_ms

Puts the current thread to sleep for the specified amount of time.

spawn

Spawns a new thread, returning a JoinHandle for it.

yield_now

Cooperatively gives up a timeslice to the OS scheduler.

catch_panic [Unstable]

Invokes a closure, capturing the cause of panic if one occurs.

park_timeout [Unstable]

Blocks unless or until the current thread's token is made available or the specified duration has been reached (may wake spuriously).

scoped [Deprecated]

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

sleep [Unstable]

Puts the current thread to sleep for the specified amount of time.

Type Definitions

Result

Indicates the manner in which a thread exited.