docs

std::thread

本页内容

API 参考只有英文版,由标准库源代码中的英文文档注释生成。

OS threads, 1:1. Each thread is a GC mutator; blocking calls let a collection run without it.

A panic in any thread ends the process (exit 101), so join never sees one; JoinError is an OS join failure. A handle neither joined nor detached keeps its OS thread record (there is no implicit drop).

THREADS maps a thread id to its Rec (name, park token, thread-local values): made before a spawned thread starts and dropped when its closure returns, or on another thread's first current() / park / thread-local use.

Structs

ThreadIdSource

pub struct ThreadId

Identifies a thread for its lifetime (its runtime worker id).

  • pub const fn as_u64(self) -> u64§

    Source

    The id as a number, unique among the threads of the process.

Trait implementations 1

ThreadSource

pub struct Thread

A thread's identity (non-generic).

  • pub const fn id(self) -> ThreadId§

    Source

    The thread's id.

  • pub const fn name(self) -> str | none§

    Source

    The name given by Builder::name; none when it has none.

  • pub fn unpark(self)§

    Source

    Wake the thread from park (or make its next park return at once). No-op once the thread has ended.

Trait implementations 1

JoinErrorSource

pub struct JoinError

The thread could not be joined (JoinError::kind).

  • pub const fn new(kind: JoinErrorKind) -> JoinError§

    Source

    An error of kind.

  • pub const fn os_code(self) -> i32 | none§

    Source

    The OS's error number of an Os kind, else none.

  • pub const fn kind(self) -> JoinErrorKind§

    Source

    Why the join failed.

  • pub const fn message(self) -> str§

    Source

    What went wrong, without context.

Trait implementations 2
  • impl Display for JoinError
  • impl Error for JoinError

JoinHandleSource

pub struct JoinHandle<T>

The running thread's result, once joined.

  • pub fn join(self) throws (JoinError) -> T§

    Source

    Wait for the thread and take its result.

  • pub const fn thread(self) -> Thread§

    Source

    The thread this handle joins.

  • pub fn detach(self)§

    Source

    Let the thread run on its own; its OS resources are released when it ends. A later join throws JoinError (Taken); a handle already joined or detached is left alone.

Trait implementations 2

BuilderSource

pub struct Builder

Spawn configuration.

  • pub const fn new() -> Builder§

    Source

    A builder with no name and the default stack size.

  • pub const fn name(self, name: str) -> Builder§

    Source

    The name of the new thread (Thread::name).

  • pub const fn stack_size(self, size: usize) -> Builder§

    Source

    Stack bytes for the new thread (default 8 MiB).

  • pub fn spawn<T: Send>(self, f: fn() -> T + Send) throws (IoError) -> JoinHandle<T>§

    Source

    Start f on a new thread with this name and stack size; throws the OS error when the thread cannot be created.

ThreadLocalSource

pub struct ThreadLocal<T>

A value per thread, made by init on each thread's first use. It lives in the thread's record and goes with it.

  • pub fn with<R>(self, f: fn(T) -> R) -> R§

    Source

    f of this thread's value. The value is a scoped view: it may not leave f (returned, stored, sent), so f is not async (E0136).

  • pub fn set(self, value: T)§

    Source

    Replace this thread's value.

  • pub fn replace(self, value: T) -> T§

    Source

    Replace this thread's value, returning the old one.

  • pub unsafe fn as_ptr(self) -> Ptr<T>§

    Source

    A pointer to this thread's value, valid until the thread ends (the slot stays pinned until then). Reads and writes through it are the caller's: nothing tracks them.

  • pub fn get(self) -> T§

    Source

    A copy of this thread's value.

  • pub fn take(self) -> T§

    Source

    This thread's value, leaving T::default() in its place.

Trait implementations 3
  • impl<T> Debug for ThreadLocal<T>
  • unsafe impl<T> Send for ThreadLocal<T>
  • unsafe impl<T> Sync for ThreadLocal<T>

Enums

JoinErrorKindSource

pub enum JoinErrorKind

Why a join failed.

  • Os(i32)§

    Source

    The OS refused: pthread_join's error number (Windows: the error code).

  • A failed REPL cell's rollback ended the thread (the session went back to before the cell; the thread is gone).

  • The handle was already joined or detached, through it or a copy of it: the OS thread handle is released once.

Trait implementations 1
  • impl Debug for JoinErrorKind

Functions

spawnSource

pub fn spawn<T: Send>(f: fn() -> T + Send) -> JoinHandle<T>

Run f on a new OS thread. Panics when the OS cannot create one (and always on wasm); Builder::spawn throws instead.

currentSource

pub fn current() -> Thread

The calling thread.

yield_nowSource

pub fn yield_now()

Let the OS run another thread.

sleepSource

pub fn sleep(dur: Duration)

Block the calling thread for dur.

panickingSource

pub const fn panicking() -> bool

Whether this thread panicked. A panic ends the process, so only code that runs while it ends (an embedder's exit hook) sees true.

parkSource

pub fn park()

Block until this thread is unparked (an earlier unpark makes it return at once). May also return spuriously: re-check the condition.

park_timeoutSource

pub fn park_timeout(dur: Duration)

park, for at most dur.

localSource

pub fn local<T>(init: fn() -> T + Sync) -> ThreadLocal<T>

A ThreadLocal whose per-thread value starts as init().