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 ThreadIdIdentifies a thread for its lifetime (its runtime worker id).
The id as a number, unique among the threads of the process.
Trait implementations 1
impl Debug for ThreadId
ThreadSource
pub struct ThreadA thread's identity (non-generic).
The thread's id.
The name given by
Builder::name;nonewhen it has none.Wake the thread from
park(or make its nextparkreturn at once). No-op once the thread has ended.
Trait implementations 1
impl Debug for Thread
JoinErrorSource
pub struct JoinErrorThe thread could not be joined (JoinError::kind).
Sourcepub const fn new(§kind: JoinErrorKind) -> JoinError An error of
kind.The OS's error number of an
Oskind, elsenone.
Sourcepub const fn kind(§self) -> JoinErrorKind Why the join failed.
What went wrong, without context.
Trait implementations 2
impl Display for JoinErrorimpl Error for JoinError
JoinHandleSource
pub struct JoinHandle<T>The running thread's result, once joined.
Wait for the thread and take its result.
The thread this handle joins.
Let the thread run on its own; its OS resources are released when it ends. A later
jointhrowsJoinError(Taken); a handle already joined or detached is left alone.
Trait implementations 2
impl<T> Debug for JoinHandle<T>impl<T: Send> IntoFuture for JoinHandle<T>
BuilderSource
pub struct BuilderSpawn configuration.
A builder with no name and the default stack size.
The name of the new thread (
Thread::name).Stack bytes for the new thread (default 8 MiB).
Start
fon 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.
fof this thread's value. The value is a scoped view: it may not leavef(returned, stored, sent), sofis notasync(E0136).Replace this thread's value.
Replace this thread's value, returning the old one.
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.
A copy of this thread's value.
This thread's value, leaving
T::default()in its place.
Enums
JoinErrorKindSource
pub enum JoinErrorKindWhy a join failed.
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().