]> git.proxmox.com Git - rustc.git/blob - library/std/src/thread/mod.rs
New upstream version 1.52.0~beta.3+dfsg1
[rustc.git] / library / std / src / thread / mod.rs
1 //! Native threads.
2 //!
3 //! ## The threading model
4 //!
5 //! An executing Rust program consists of a collection of native OS threads,
6 //! each with their own stack and local state. Threads can be named, and
7 //! provide some built-in support for low-level synchronization.
8 //!
9 //! Communication between threads can be done through
10 //! [channels], Rust's message-passing types, along with [other forms of thread
11 //! synchronization](../../std/sync/index.html) and shared-memory data
12 //! structures. In particular, types that are guaranteed to be
13 //! threadsafe are easily shared between threads using the
14 //! atomically-reference-counted container, [`Arc`].
15 //!
16 //! Fatal logic errors in Rust cause *thread panic*, during which
17 //! a thread will unwind the stack, running destructors and freeing
18 //! owned resources. While not meant as a 'try/catch' mechanism, panics
19 //! in Rust can nonetheless be caught (unless compiling with `panic=abort`) with
20 //! [`catch_unwind`](../../std/panic/fn.catch_unwind.html) and recovered
21 //! from, or alternatively be resumed with
22 //! [`resume_unwind`](../../std/panic/fn.resume_unwind.html). If the panic
23 //! is not caught the thread will exit, but the panic may optionally be
24 //! detected from a different thread with [`join`]. If the main thread panics
25 //! without the panic being caught, the application will exit with a
26 //! non-zero exit code.
27 //!
28 //! When the main thread of a Rust program terminates, the entire program shuts
29 //! down, even if other threads are still running. However, this module provides
30 //! convenient facilities for automatically waiting for the termination of a
31 //! child thread (i.e., join).
32 //!
33 //! ## Spawning a thread
34 //!
35 //! A new thread can be spawned using the [`thread::spawn`][`spawn`] function:
36 //!
37 //! ```rust
38 //! use std::thread;
39 //!
40 //! thread::spawn(move || {
41 //! // some work here
42 //! });
43 //! ```
44 //!
45 //! In this example, the spawned thread is "detached" from the current
46 //! thread. This means that it can outlive its parent (the thread that spawned
47 //! it), unless this parent is the main thread.
48 //!
49 //! The parent thread can also wait on the completion of the child
50 //! thread; a call to [`spawn`] produces a [`JoinHandle`], which provides
51 //! a `join` method for waiting:
52 //!
53 //! ```rust
54 //! use std::thread;
55 //!
56 //! let child = thread::spawn(move || {
57 //! // some work here
58 //! });
59 //! // some work here
60 //! let res = child.join();
61 //! ```
62 //!
63 //! The [`join`] method returns a [`thread::Result`] containing [`Ok`] of the final
64 //! value produced by the child thread, or [`Err`] of the value given to
65 //! a call to [`panic!`] if the child panicked.
66 //!
67 //! ## Configuring threads
68 //!
69 //! A new thread can be configured before it is spawned via the [`Builder`] type,
70 //! which currently allows you to set the name and stack size for the child thread:
71 //!
72 //! ```rust
73 //! # #![allow(unused_must_use)]
74 //! use std::thread;
75 //!
76 //! thread::Builder::new().name("child1".to_string()).spawn(move || {
77 //! println!("Hello, world!");
78 //! });
79 //! ```
80 //!
81 //! ## The `Thread` type
82 //!
83 //! Threads are represented via the [`Thread`] type, which you can get in one of
84 //! two ways:
85 //!
86 //! * By spawning a new thread, e.g., using the [`thread::spawn`][`spawn`]
87 //! function, and calling [`thread`][`JoinHandle::thread`] on the [`JoinHandle`].
88 //! * By requesting the current thread, using the [`thread::current`] function.
89 //!
90 //! The [`thread::current`] function is available even for threads not spawned
91 //! by the APIs of this module.
92 //!
93 //! ## Thread-local storage
94 //!
95 //! This module also provides an implementation of thread-local storage for Rust
96 //! programs. Thread-local storage is a method of storing data into a global
97 //! variable that each thread in the program will have its own copy of.
98 //! Threads do not share this data, so accesses do not need to be synchronized.
99 //!
100 //! A thread-local key owns the value it contains and will destroy the value when the
101 //! thread exits. It is created with the [`thread_local!`] macro and can contain any
102 //! value that is `'static` (no borrowed pointers). It provides an accessor function,
103 //! [`with`], that yields a shared reference to the value to the specified
104 //! closure. Thread-local keys allow only shared access to values, as there would be no
105 //! way to guarantee uniqueness if mutable borrows were allowed. Most values
106 //! will want to make use of some form of **interior mutability** through the
107 //! [`Cell`] or [`RefCell`] types.
108 //!
109 //! ## Naming threads
110 //!
111 //! Threads are able to have associated names for identification purposes. By default, spawned
112 //! threads are unnamed. To specify a name for a thread, build the thread with [`Builder`] and pass
113 //! the desired thread name to [`Builder::name`]. To retrieve the thread name from within the
114 //! thread, use [`Thread::name`]. A couple examples of where the name of a thread gets used:
115 //!
116 //! * If a panic occurs in a named thread, the thread name will be printed in the panic message.
117 //! * The thread name is provided to the OS where applicable (e.g., `pthread_setname_np` in
118 //! unix-like platforms).
119 //!
120 //! ## Stack size
121 //!
122 //! The default stack size for spawned threads is 2 MiB, though this particular stack size is
123 //! subject to change in the future. There are two ways to manually specify the stack size for
124 //! spawned threads:
125 //!
126 //! * Build the thread with [`Builder`] and pass the desired stack size to [`Builder::stack_size`].
127 //! * Set the `RUST_MIN_STACK` environment variable to an integer representing the desired stack
128 //! size (in bytes). Note that setting [`Builder::stack_size`] will override this.
129 //!
130 //! Note that the stack size of the main thread is *not* determined by Rust.
131 //!
132 //! [channels]: crate::sync::mpsc
133 //! [`join`]: JoinHandle::join
134 //! [`Result`]: crate::result::Result
135 //! [`Ok`]: crate::result::Result::Ok
136 //! [`Err`]: crate::result::Result::Err
137 //! [`thread::current`]: current
138 //! [`thread::Result`]: Result
139 //! [`unpark`]: Thread::unpark
140 //! [`thread::park_timeout`]: park_timeout
141 //! [`Cell`]: crate::cell::Cell
142 //! [`RefCell`]: crate::cell::RefCell
143 //! [`with`]: LocalKey::with
144
145 #![stable(feature = "rust1", since = "1.0.0")]
146 #![deny(unsafe_op_in_unsafe_fn)]
147
148 #[cfg(all(test, not(target_os = "emscripten")))]
149 mod tests;
150
151 use crate::any::Any;
152 use crate::cell::UnsafeCell;
153 use crate::ffi::{CStr, CString};
154 use crate::fmt;
155 use crate::io;
156 use crate::mem;
157 use crate::num::NonZeroU64;
158 use crate::panic;
159 use crate::panicking;
160 use crate::str;
161 use crate::sync::Arc;
162 use crate::sys::thread as imp;
163 use crate::sys_common::mutex;
164 use crate::sys_common::thread;
165 use crate::sys_common::thread_info;
166 use crate::sys_common::thread_parker::Parker;
167 use crate::sys_common::{AsInner, IntoInner};
168 use crate::time::Duration;
169
170 ////////////////////////////////////////////////////////////////////////////////
171 // Thread-local storage
172 ////////////////////////////////////////////////////////////////////////////////
173
174 #[macro_use]
175 mod local;
176
177 #[unstable(feature = "available_concurrency", issue = "74479")]
178 mod available_concurrency;
179
180 #[stable(feature = "rust1", since = "1.0.0")]
181 pub use self::local::{AccessError, LocalKey};
182
183 #[unstable(feature = "available_concurrency", issue = "74479")]
184 pub use available_concurrency::available_concurrency;
185
186 // The types used by the thread_local! macro to access TLS keys. Note that there
187 // are two types, the "OS" type and the "fast" type. The OS thread local key
188 // type is accessed via platform-specific API calls and is slow, while the fast
189 // key type is accessed via code generated via LLVM, where TLS keys are set up
190 // by the elf linker. Note that the OS TLS type is always available: on macOS
191 // the standard library is compiled with support for older platform versions
192 // where fast TLS was not available; end-user code is compiled with fast TLS
193 // where available, but both are needed.
194
195 #[unstable(feature = "libstd_thread_internals", issue = "none")]
196 #[cfg(target_thread_local)]
197 #[doc(hidden)]
198 pub use self::local::fast::Key as __FastLocalKeyInner;
199 #[unstable(feature = "libstd_thread_internals", issue = "none")]
200 #[doc(hidden)]
201 pub use self::local::os::Key as __OsLocalKeyInner;
202 #[unstable(feature = "libstd_thread_internals", issue = "none")]
203 #[cfg(all(target_arch = "wasm32", not(target_feature = "atomics")))]
204 #[doc(hidden)]
205 pub use self::local::statik::Key as __StaticLocalKeyInner;
206
207 ////////////////////////////////////////////////////////////////////////////////
208 // Builder
209 ////////////////////////////////////////////////////////////////////////////////
210
211 /// Thread factory, which can be used in order to configure the properties of
212 /// a new thread.
213 ///
214 /// Methods can be chained on it in order to configure it.
215 ///
216 /// The two configurations available are:
217 ///
218 /// - [`name`]: specifies an [associated name for the thread][naming-threads]
219 /// - [`stack_size`]: specifies the [desired stack size for the thread][stack-size]
220 ///
221 /// The [`spawn`] method will take ownership of the builder and create an
222 /// [`io::Result`] to the thread handle with the given configuration.
223 ///
224 /// The [`thread::spawn`] free function uses a `Builder` with default
225 /// configuration and [`unwrap`]s its return value.
226 ///
227 /// You may want to use [`spawn`] instead of [`thread::spawn`], when you want
228 /// to recover from a failure to launch a thread, indeed the free function will
229 /// panic where the `Builder` method will return a [`io::Result`].
230 ///
231 /// # Examples
232 ///
233 /// ```
234 /// use std::thread;
235 ///
236 /// let builder = thread::Builder::new();
237 ///
238 /// let handler = builder.spawn(|| {
239 /// // thread code
240 /// }).unwrap();
241 ///
242 /// handler.join().unwrap();
243 /// ```
244 ///
245 /// [`stack_size`]: Builder::stack_size
246 /// [`name`]: Builder::name
247 /// [`spawn`]: Builder::spawn
248 /// [`thread::spawn`]: spawn
249 /// [`io::Result`]: crate::io::Result
250 /// [`unwrap`]: crate::result::Result::unwrap
251 /// [naming-threads]: ./index.html#naming-threads
252 /// [stack-size]: ./index.html#stack-size
253 #[stable(feature = "rust1", since = "1.0.0")]
254 #[derive(Debug)]
255 pub struct Builder {
256 // A name for the thread-to-be, for identification in panic messages
257 name: Option<String>,
258 // The size of the stack for the spawned thread in bytes
259 stack_size: Option<usize>,
260 }
261
262 impl Builder {
263 /// Generates the base configuration for spawning a thread, from which
264 /// configuration methods can be chained.
265 ///
266 /// # Examples
267 ///
268 /// ```
269 /// use std::thread;
270 ///
271 /// let builder = thread::Builder::new()
272 /// .name("foo".into())
273 /// .stack_size(32 * 1024);
274 ///
275 /// let handler = builder.spawn(|| {
276 /// // thread code
277 /// }).unwrap();
278 ///
279 /// handler.join().unwrap();
280 /// ```
281 #[stable(feature = "rust1", since = "1.0.0")]
282 pub fn new() -> Builder {
283 Builder { name: None, stack_size: None }
284 }
285
286 /// Names the thread-to-be. Currently the name is used for identification
287 /// only in panic messages.
288 ///
289 /// The name must not contain null bytes (`\0`).
290 ///
291 /// For more information about named threads, see
292 /// [this module-level documentation][naming-threads].
293 ///
294 /// # Examples
295 ///
296 /// ```
297 /// use std::thread;
298 ///
299 /// let builder = thread::Builder::new()
300 /// .name("foo".into());
301 ///
302 /// let handler = builder.spawn(|| {
303 /// assert_eq!(thread::current().name(), Some("foo"))
304 /// }).unwrap();
305 ///
306 /// handler.join().unwrap();
307 /// ```
308 ///
309 /// [naming-threads]: ./index.html#naming-threads
310 #[stable(feature = "rust1", since = "1.0.0")]
311 pub fn name(mut self, name: String) -> Builder {
312 self.name = Some(name);
313 self
314 }
315
316 /// Sets the size of the stack (in bytes) for the new thread.
317 ///
318 /// The actual stack size may be greater than this value if
319 /// the platform specifies a minimal stack size.
320 ///
321 /// For more information about the stack size for threads, see
322 /// [this module-level documentation][stack-size].
323 ///
324 /// # Examples
325 ///
326 /// ```
327 /// use std::thread;
328 ///
329 /// let builder = thread::Builder::new().stack_size(32 * 1024);
330 /// ```
331 ///
332 /// [stack-size]: ./index.html#stack-size
333 #[stable(feature = "rust1", since = "1.0.0")]
334 pub fn stack_size(mut self, size: usize) -> Builder {
335 self.stack_size = Some(size);
336 self
337 }
338
339 /// Spawns a new thread by taking ownership of the `Builder`, and returns an
340 /// [`io::Result`] to its [`JoinHandle`].
341 ///
342 /// The spawned thread may outlive the caller (unless the caller thread
343 /// is the main thread; the whole process is terminated when the main
344 /// thread finishes). The join handle can be used to block on
345 /// termination of the child thread, including recovering its panics.
346 ///
347 /// For a more complete documentation see [`thread::spawn`][`spawn`].
348 ///
349 /// # Errors
350 ///
351 /// Unlike the [`spawn`] free function, this method yields an
352 /// [`io::Result`] to capture any failure to create the thread at
353 /// the OS level.
354 ///
355 /// [`io::Result`]: crate::io::Result
356 ///
357 /// # Panics
358 ///
359 /// Panics if a thread name was set and it contained null bytes.
360 ///
361 /// # Examples
362 ///
363 /// ```
364 /// use std::thread;
365 ///
366 /// let builder = thread::Builder::new();
367 ///
368 /// let handler = builder.spawn(|| {
369 /// // thread code
370 /// }).unwrap();
371 ///
372 /// handler.join().unwrap();
373 /// ```
374 #[stable(feature = "rust1", since = "1.0.0")]
375 pub fn spawn<F, T>(self, f: F) -> io::Result<JoinHandle<T>>
376 where
377 F: FnOnce() -> T,
378 F: Send + 'static,
379 T: Send + 'static,
380 {
381 unsafe { self.spawn_unchecked(f) }
382 }
383
384 /// Spawns a new thread without any lifetime restrictions by taking ownership
385 /// of the `Builder`, and returns an [`io::Result`] to its [`JoinHandle`].
386 ///
387 /// The spawned thread may outlive the caller (unless the caller thread
388 /// is the main thread; the whole process is terminated when the main
389 /// thread finishes). The join handle can be used to block on
390 /// termination of the child thread, including recovering its panics.
391 ///
392 /// This method is identical to [`thread::Builder::spawn`][`Builder::spawn`],
393 /// except for the relaxed lifetime bounds, which render it unsafe.
394 /// For a more complete documentation see [`thread::spawn`][`spawn`].
395 ///
396 /// # Errors
397 ///
398 /// Unlike the [`spawn`] free function, this method yields an
399 /// [`io::Result`] to capture any failure to create the thread at
400 /// the OS level.
401 ///
402 /// # Panics
403 ///
404 /// Panics if a thread name was set and it contained null bytes.
405 ///
406 /// # Safety
407 ///
408 /// The caller has to ensure that no references in the supplied thread closure
409 /// or its return type can outlive the spawned thread's lifetime. This can be
410 /// guaranteed in two ways:
411 ///
412 /// - ensure that [`join`][`JoinHandle::join`] is called before any referenced
413 /// data is dropped
414 /// - use only types with `'static` lifetime bounds, i.e., those with no or only
415 /// `'static` references (both [`thread::Builder::spawn`][`Builder::spawn`]
416 /// and [`thread::spawn`][`spawn`] enforce this property statically)
417 ///
418 /// # Examples
419 ///
420 /// ```
421 /// #![feature(thread_spawn_unchecked)]
422 /// use std::thread;
423 ///
424 /// let builder = thread::Builder::new();
425 ///
426 /// let x = 1;
427 /// let thread_x = &x;
428 ///
429 /// let handler = unsafe {
430 /// builder.spawn_unchecked(move || {
431 /// println!("x = {}", *thread_x);
432 /// }).unwrap()
433 /// };
434 ///
435 /// // caller has to ensure `join()` is called, otherwise
436 /// // it is possible to access freed memory if `x` gets
437 /// // dropped before the thread closure is executed!
438 /// handler.join().unwrap();
439 /// ```
440 ///
441 /// [`io::Result`]: crate::io::Result
442 #[unstable(feature = "thread_spawn_unchecked", issue = "55132")]
443 pub unsafe fn spawn_unchecked<'a, F, T>(self, f: F) -> io::Result<JoinHandle<T>>
444 where
445 F: FnOnce() -> T,
446 F: Send + 'a,
447 T: Send + 'a,
448 {
449 let Builder { name, stack_size } = self;
450
451 let stack_size = stack_size.unwrap_or_else(thread::min_stack);
452
453 let my_thread = Thread::new(name);
454 let their_thread = my_thread.clone();
455
456 let my_packet: Arc<UnsafeCell<Option<Result<T>>>> = Arc::new(UnsafeCell::new(None));
457 let their_packet = my_packet.clone();
458
459 let output_capture = crate::io::set_output_capture(None);
460 crate::io::set_output_capture(output_capture.clone());
461
462 let main = move || {
463 if let Some(name) = their_thread.cname() {
464 imp::Thread::set_name(name);
465 }
466
467 crate::io::set_output_capture(output_capture);
468
469 // SAFETY: the stack guard passed is the one for the current thread.
470 // This means the current thread's stack and the new thread's stack
471 // are properly set and protected from each other.
472 thread_info::set(unsafe { imp::guard::current() }, their_thread);
473 let try_result = panic::catch_unwind(panic::AssertUnwindSafe(|| {
474 crate::sys_common::backtrace::__rust_begin_short_backtrace(f)
475 }));
476 // SAFETY: `their_packet` as been built just above and moved by the
477 // closure (it is an Arc<...>) and `my_packet` will be stored in the
478 // same `JoinInner` as this closure meaning the mutation will be
479 // safe (not modify it and affect a value far away).
480 unsafe { *their_packet.get() = Some(try_result) };
481 };
482
483 Ok(JoinHandle(JoinInner {
484 // SAFETY:
485 //
486 // `imp::Thread::new` takes a closure with a `'static` lifetime, since it's passed
487 // through FFI or otherwise used with low-level threading primitives that have no
488 // notion of or way to enforce lifetimes.
489 //
490 // As mentioned in the `Safety` section of this function's documentation, the caller of
491 // this function needs to guarantee that the passed-in lifetime is sufficiently long
492 // for the lifetime of the thread.
493 //
494 // Similarly, the `sys` implementation must guarantee that no references to the closure
495 // exist after the thread has terminated, which is signaled by `Thread::join`
496 // returning.
497 native: unsafe {
498 Some(imp::Thread::new(
499 stack_size,
500 mem::transmute::<Box<dyn FnOnce() + 'a>, Box<dyn FnOnce() + 'static>>(
501 Box::new(main),
502 ),
503 )?)
504 },
505 thread: my_thread,
506 packet: Packet(my_packet),
507 }))
508 }
509 }
510
511 ////////////////////////////////////////////////////////////////////////////////
512 // Free functions
513 ////////////////////////////////////////////////////////////////////////////////
514
515 /// Spawns a new thread, returning a [`JoinHandle`] for it.
516 ///
517 /// The join handle will implicitly *detach* the child thread upon being
518 /// dropped. In this case, the child thread may outlive the parent (unless
519 /// the parent thread is the main thread; the whole process is terminated when
520 /// the main thread finishes). Additionally, the join handle provides a [`join`]
521 /// method that can be used to join the child thread. If the child thread
522 /// panics, [`join`] will return an [`Err`] containing the argument given to
523 /// [`panic!`].
524 ///
525 /// This will create a thread using default parameters of [`Builder`], if you
526 /// want to specify the stack size or the name of the thread, use this API
527 /// instead.
528 ///
529 /// As you can see in the signature of `spawn` there are two constraints on
530 /// both the closure given to `spawn` and its return value, let's explain them:
531 ///
532 /// - The `'static` constraint means that the closure and its return value
533 /// must have a lifetime of the whole program execution. The reason for this
534 /// is that threads can `detach` and outlive the lifetime they have been
535 /// created in.
536 /// Indeed if the thread, and by extension its return value, can outlive their
537 /// caller, we need to make sure that they will be valid afterwards, and since
538 /// we *can't* know when it will return we need to have them valid as long as
539 /// possible, that is until the end of the program, hence the `'static`
540 /// lifetime.
541 /// - The [`Send`] constraint is because the closure will need to be passed
542 /// *by value* from the thread where it is spawned to the new thread. Its
543 /// return value will need to be passed from the new thread to the thread
544 /// where it is `join`ed.
545 /// As a reminder, the [`Send`] marker trait expresses that it is safe to be
546 /// passed from thread to thread. [`Sync`] expresses that it is safe to have a
547 /// reference be passed from thread to thread.
548 ///
549 /// # Panics
550 ///
551 /// Panics if the OS fails to create a thread; use [`Builder::spawn`]
552 /// to recover from such errors.
553 ///
554 /// # Examples
555 ///
556 /// Creating a thread.
557 ///
558 /// ```
559 /// use std::thread;
560 ///
561 /// let handler = thread::spawn(|| {
562 /// // thread code
563 /// });
564 ///
565 /// handler.join().unwrap();
566 /// ```
567 ///
568 /// As mentioned in the module documentation, threads are usually made to
569 /// communicate using [`channels`], here is how it usually looks.
570 ///
571 /// This example also shows how to use `move`, in order to give ownership
572 /// of values to a thread.
573 ///
574 /// ```
575 /// use std::thread;
576 /// use std::sync::mpsc::channel;
577 ///
578 /// let (tx, rx) = channel();
579 ///
580 /// let sender = thread::spawn(move || {
581 /// tx.send("Hello, thread".to_owned())
582 /// .expect("Unable to send on channel");
583 /// });
584 ///
585 /// let receiver = thread::spawn(move || {
586 /// let value = rx.recv().expect("Unable to receive from channel");
587 /// println!("{}", value);
588 /// });
589 ///
590 /// sender.join().expect("The sender thread has panicked");
591 /// receiver.join().expect("The receiver thread has panicked");
592 /// ```
593 ///
594 /// A thread can also return a value through its [`JoinHandle`], you can use
595 /// this to make asynchronous computations (futures might be more appropriate
596 /// though).
597 ///
598 /// ```
599 /// use std::thread;
600 ///
601 /// let computation = thread::spawn(|| {
602 /// // Some expensive computation.
603 /// 42
604 /// });
605 ///
606 /// let result = computation.join().unwrap();
607 /// println!("{}", result);
608 /// ```
609 ///
610 /// [`channels`]: crate::sync::mpsc
611 /// [`join`]: JoinHandle::join
612 /// [`Err`]: crate::result::Result::Err
613 #[stable(feature = "rust1", since = "1.0.0")]
614 pub fn spawn<F, T>(f: F) -> JoinHandle<T>
615 where
616 F: FnOnce() -> T,
617 F: Send + 'static,
618 T: Send + 'static,
619 {
620 Builder::new().spawn(f).expect("failed to spawn thread")
621 }
622
623 /// Gets a handle to the thread that invokes it.
624 ///
625 /// # Examples
626 ///
627 /// Getting a handle to the current thread with `thread::current()`:
628 ///
629 /// ```
630 /// use std::thread;
631 ///
632 /// let handler = thread::Builder::new()
633 /// .name("named thread".into())
634 /// .spawn(|| {
635 /// let handle = thread::current();
636 /// assert_eq!(handle.name(), Some("named thread"));
637 /// })
638 /// .unwrap();
639 ///
640 /// handler.join().unwrap();
641 /// ```
642 #[stable(feature = "rust1", since = "1.0.0")]
643 pub fn current() -> Thread {
644 thread_info::current_thread().expect(
645 "use of std::thread::current() is not possible \
646 after the thread's local data has been destroyed",
647 )
648 }
649
650 /// Cooperatively gives up a timeslice to the OS scheduler.
651 ///
652 /// This is used when the programmer knows that the thread will have nothing
653 /// to do for some time, and thus avoid wasting computing time.
654 ///
655 /// For example when polling on a resource, it is common to check that it is
656 /// available, and if not to yield in order to avoid busy waiting.
657 ///
658 /// Thus the pattern of `yield`ing after a failed poll is rather common when
659 /// implementing low-level shared resources or synchronization primitives.
660 ///
661 /// However programmers will usually prefer to use [`channel`]s, [`Condvar`]s,
662 /// [`Mutex`]es or [`join`] for their synchronization routines, as they avoid
663 /// thinking about thread scheduling.
664 ///
665 /// Note that [`channel`]s for example are implemented using this primitive.
666 /// Indeed when you call `send` or `recv`, which are blocking, they will yield
667 /// if the channel is not available.
668 ///
669 /// # Examples
670 ///
671 /// ```
672 /// use std::thread;
673 ///
674 /// thread::yield_now();
675 /// ```
676 ///
677 /// [`channel`]: crate::sync::mpsc
678 /// [`join`]: JoinHandle::join
679 /// [`Condvar`]: crate::sync::Condvar
680 /// [`Mutex`]: crate::sync::Mutex
681 #[stable(feature = "rust1", since = "1.0.0")]
682 pub fn yield_now() {
683 imp::Thread::yield_now()
684 }
685
686 /// Determines whether the current thread is unwinding because of panic.
687 ///
688 /// A common use of this feature is to poison shared resources when writing
689 /// unsafe code, by checking `panicking` when the `drop` is called.
690 ///
691 /// This is usually not needed when writing safe code, as [`Mutex`es][Mutex]
692 /// already poison themselves when a thread panics while holding the lock.
693 ///
694 /// This can also be used in multithreaded applications, in order to send a
695 /// message to other threads warning that a thread has panicked (e.g., for
696 /// monitoring purposes).
697 ///
698 /// # Examples
699 ///
700 /// ```should_panic
701 /// use std::thread;
702 ///
703 /// struct SomeStruct;
704 ///
705 /// impl Drop for SomeStruct {
706 /// fn drop(&mut self) {
707 /// if thread::panicking() {
708 /// println!("dropped while unwinding");
709 /// } else {
710 /// println!("dropped while not unwinding");
711 /// }
712 /// }
713 /// }
714 ///
715 /// {
716 /// print!("a: ");
717 /// let a = SomeStruct;
718 /// }
719 ///
720 /// {
721 /// print!("b: ");
722 /// let b = SomeStruct;
723 /// panic!()
724 /// }
725 /// ```
726 ///
727 /// [Mutex]: crate::sync::Mutex
728 #[inline]
729 #[stable(feature = "rust1", since = "1.0.0")]
730 pub fn panicking() -> bool {
731 panicking::panicking()
732 }
733
734 /// Puts the current thread to sleep for at least the specified amount of time.
735 ///
736 /// The thread may sleep longer than the duration specified due to scheduling
737 /// specifics or platform-dependent functionality. It will never sleep less.
738 ///
739 /// This function is blocking, and should not be used in `async` functions.
740 ///
741 /// # Platform-specific behavior
742 ///
743 /// On Unix platforms, the underlying syscall may be interrupted by a
744 /// spurious wakeup or signal handler. To ensure the sleep occurs for at least
745 /// the specified duration, this function may invoke that system call multiple
746 /// times.
747 ///
748 /// # Examples
749 ///
750 /// ```no_run
751 /// use std::thread;
752 ///
753 /// // Let's sleep for 2 seconds:
754 /// thread::sleep_ms(2000);
755 /// ```
756 #[stable(feature = "rust1", since = "1.0.0")]
757 #[rustc_deprecated(since = "1.6.0", reason = "replaced by `std::thread::sleep`")]
758 pub fn sleep_ms(ms: u32) {
759 sleep(Duration::from_millis(ms as u64))
760 }
761
762 /// Puts the current thread to sleep for at least the specified amount of time.
763 ///
764 /// The thread may sleep longer than the duration specified due to scheduling
765 /// specifics or platform-dependent functionality. It will never sleep less.
766 ///
767 /// This function is blocking, and should not be used in `async` functions.
768 ///
769 /// # Platform-specific behavior
770 ///
771 /// On Unix platforms, the underlying syscall may be interrupted by a
772 /// spurious wakeup or signal handler. To ensure the sleep occurs for at least
773 /// the specified duration, this function may invoke that system call multiple
774 /// times.
775 /// Platforms which do not support nanosecond precision for sleeping will
776 /// have `dur` rounded up to the nearest granularity of time they can sleep for.
777 ///
778 /// Currently, specifying a zero duration on Unix platforms returns immediately
779 /// without invoking the underlying [`nanosleep`] syscall, whereas on Windows
780 /// platforms the underlying [`Sleep`] syscall is always invoked.
781 /// If the intention is to yield the current time-slice you may want to use
782 /// [`yield_now`] instead.
783 ///
784 /// [`nanosleep`]: https://linux.die.net/man/2/nanosleep
785 /// [`Sleep`]: https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-sleep
786 ///
787 /// # Examples
788 ///
789 /// ```no_run
790 /// use std::{thread, time};
791 ///
792 /// let ten_millis = time::Duration::from_millis(10);
793 /// let now = time::Instant::now();
794 ///
795 /// thread::sleep(ten_millis);
796 ///
797 /// assert!(now.elapsed() >= ten_millis);
798 /// ```
799 #[stable(feature = "thread_sleep", since = "1.4.0")]
800 pub fn sleep(dur: Duration) {
801 imp::Thread::sleep(dur)
802 }
803
804 /// Blocks unless or until the current thread's token is made available.
805 ///
806 /// A call to `park` does not guarantee that the thread will remain parked
807 /// forever, and callers should be prepared for this possibility.
808 ///
809 /// # park and unpark
810 ///
811 /// Every thread is equipped with some basic low-level blocking support, via the
812 /// [`thread::park`][`park`] function and [`thread::Thread::unpark`][`unpark`]
813 /// method. [`park`] blocks the current thread, which can then be resumed from
814 /// another thread by calling the [`unpark`] method on the blocked thread's
815 /// handle.
816 ///
817 /// Conceptually, each [`Thread`] handle has an associated token, which is
818 /// initially not present:
819 ///
820 /// * The [`thread::park`][`park`] function blocks the current thread unless or
821 /// until the token is available for its thread handle, at which point it
822 /// atomically consumes the token. It may also return *spuriously*, without
823 /// consuming the token. [`thread::park_timeout`] does the same, but allows
824 /// specifying a maximum time to block the thread for.
825 ///
826 /// * The [`unpark`] method on a [`Thread`] atomically makes the token available
827 /// if it wasn't already. Because the token is initially absent, [`unpark`]
828 /// followed by [`park`] will result in the second call returning immediately.
829 ///
830 /// In other words, each [`Thread`] acts a bit like a spinlock that can be
831 /// locked and unlocked using `park` and `unpark`.
832 ///
833 /// Notice that being unblocked does not imply any synchronization with someone
834 /// that unparked this thread, it could also be spurious.
835 /// For example, it would be a valid, but inefficient, implementation to make both [`park`] and
836 /// [`unpark`] return immediately without doing anything.
837 ///
838 /// The API is typically used by acquiring a handle to the current thread,
839 /// placing that handle in a shared data structure so that other threads can
840 /// find it, and then `park`ing in a loop. When some desired condition is met, another
841 /// thread calls [`unpark`] on the handle.
842 ///
843 /// The motivation for this design is twofold:
844 ///
845 /// * It avoids the need to allocate mutexes and condvars when building new
846 /// synchronization primitives; the threads already provide basic
847 /// blocking/signaling.
848 ///
849 /// * It can be implemented very efficiently on many platforms.
850 ///
851 /// # Examples
852 ///
853 /// ```
854 /// use std::thread;
855 /// use std::sync::{Arc, atomic::{Ordering, AtomicBool}};
856 /// use std::time::Duration;
857 ///
858 /// let flag = Arc::new(AtomicBool::new(false));
859 /// let flag2 = Arc::clone(&flag);
860 ///
861 /// let parked_thread = thread::spawn(move || {
862 /// // We want to wait until the flag is set. We *could* just spin, but using
863 /// // park/unpark is more efficient.
864 /// while !flag2.load(Ordering::Acquire) {
865 /// println!("Parking thread");
866 /// thread::park();
867 /// // We *could* get here spuriously, i.e., way before the 10ms below are over!
868 /// // But that is no problem, we are in a loop until the flag is set anyway.
869 /// println!("Thread unparked");
870 /// }
871 /// println!("Flag received");
872 /// });
873 ///
874 /// // Let some time pass for the thread to be spawned.
875 /// thread::sleep(Duration::from_millis(10));
876 ///
877 /// // Set the flag, and let the thread wake up.
878 /// // There is no race condition here, if `unpark`
879 /// // happens first, `park` will return immediately.
880 /// // Hence there is no risk of a deadlock.
881 /// flag.store(true, Ordering::Release);
882 /// println!("Unpark the thread");
883 /// parked_thread.thread().unpark();
884 ///
885 /// parked_thread.join().unwrap();
886 /// ```
887 ///
888 /// [`unpark`]: Thread::unpark
889 /// [`thread::park_timeout`]: park_timeout
890 #[stable(feature = "rust1", since = "1.0.0")]
891 pub fn park() {
892 // SAFETY: park_timeout is called on the parker owned by this thread.
893 unsafe {
894 current().inner.parker.park();
895 }
896 }
897
898 /// Use [`park_timeout`].
899 ///
900 /// Blocks unless or until the current thread's token is made available or
901 /// the specified duration has been reached (may wake spuriously).
902 ///
903 /// The semantics of this function are equivalent to [`park`] except
904 /// that the thread will be blocked for roughly no longer than `dur`. This
905 /// method should not be used for precise timing due to anomalies such as
906 /// preemption or platform differences that may not cause the maximum
907 /// amount of time waited to be precisely `ms` long.
908 ///
909 /// See the [park documentation][`park`] for more detail.
910 #[stable(feature = "rust1", since = "1.0.0")]
911 #[rustc_deprecated(since = "1.6.0", reason = "replaced by `std::thread::park_timeout`")]
912 pub fn park_timeout_ms(ms: u32) {
913 park_timeout(Duration::from_millis(ms as u64))
914 }
915
916 /// Blocks unless or until the current thread's token is made available or
917 /// the specified duration has been reached (may wake spuriously).
918 ///
919 /// The semantics of this function are equivalent to [`park`][park] except
920 /// that the thread will be blocked for roughly no longer than `dur`. This
921 /// method should not be used for precise timing due to anomalies such as
922 /// preemption or platform differences that may not cause the maximum
923 /// amount of time waited to be precisely `dur` long.
924 ///
925 /// See the [park documentation][park] for more details.
926 ///
927 /// # Platform-specific behavior
928 ///
929 /// Platforms which do not support nanosecond precision for sleeping will have
930 /// `dur` rounded up to the nearest granularity of time they can sleep for.
931 ///
932 /// # Examples
933 ///
934 /// Waiting for the complete expiration of the timeout:
935 ///
936 /// ```rust,no_run
937 /// use std::thread::park_timeout;
938 /// use std::time::{Instant, Duration};
939 ///
940 /// let timeout = Duration::from_secs(2);
941 /// let beginning_park = Instant::now();
942 ///
943 /// let mut timeout_remaining = timeout;
944 /// loop {
945 /// park_timeout(timeout_remaining);
946 /// let elapsed = beginning_park.elapsed();
947 /// if elapsed >= timeout {
948 /// break;
949 /// }
950 /// println!("restarting park_timeout after {:?}", elapsed);
951 /// timeout_remaining = timeout - elapsed;
952 /// }
953 /// ```
954 #[stable(feature = "park_timeout", since = "1.4.0")]
955 pub fn park_timeout(dur: Duration) {
956 // SAFETY: park_timeout is called on the parker owned by this thread.
957 unsafe {
958 current().inner.parker.park_timeout(dur);
959 }
960 }
961
962 ////////////////////////////////////////////////////////////////////////////////
963 // ThreadId
964 ////////////////////////////////////////////////////////////////////////////////
965
966 /// A unique identifier for a running thread.
967 ///
968 /// A `ThreadId` is an opaque object that has a unique value for each thread
969 /// that creates one. `ThreadId`s are not guaranteed to correspond to a thread's
970 /// system-designated identifier. A `ThreadId` can be retrieved from the [`id`]
971 /// method on a [`Thread`].
972 ///
973 /// # Examples
974 ///
975 /// ```
976 /// use std::thread;
977 ///
978 /// let other_thread = thread::spawn(|| {
979 /// thread::current().id()
980 /// });
981 ///
982 /// let other_thread_id = other_thread.join().unwrap();
983 /// assert!(thread::current().id() != other_thread_id);
984 /// ```
985 ///
986 /// [`id`]: Thread::id
987 #[stable(feature = "thread_id", since = "1.19.0")]
988 #[derive(Eq, PartialEq, Clone, Copy, Hash, Debug)]
989 pub struct ThreadId(NonZeroU64);
990
991 impl ThreadId {
992 // Generate a new unique thread ID.
993 fn new() -> ThreadId {
994 // It is UB to attempt to acquire this mutex reentrantly!
995 static GUARD: mutex::StaticMutex = mutex::StaticMutex::new();
996 static mut COUNTER: u64 = 1;
997
998 unsafe {
999 let _guard = GUARD.lock();
1000
1001 // If we somehow use up all our bits, panic so that we're not
1002 // covering up subtle bugs of IDs being reused.
1003 if COUNTER == u64::MAX {
1004 panic!("failed to generate unique thread ID: bitspace exhausted");
1005 }
1006
1007 let id = COUNTER;
1008 COUNTER += 1;
1009
1010 ThreadId(NonZeroU64::new(id).unwrap())
1011 }
1012 }
1013
1014 /// This returns a numeric identifier for the thread identified by this
1015 /// `ThreadId`.
1016 ///
1017 /// As noted in the documentation for the type itself, it is essentially an
1018 /// opaque ID, but is guaranteed to be unique for each thread. The returned
1019 /// value is entirely opaque -- only equality testing is stable. Note that
1020 /// it is not guaranteed which values new threads will return, and this may
1021 /// change across Rust versions.
1022 #[unstable(feature = "thread_id_value", issue = "67939")]
1023 pub fn as_u64(&self) -> NonZeroU64 {
1024 self.0
1025 }
1026 }
1027
1028 ////////////////////////////////////////////////////////////////////////////////
1029 // Thread
1030 ////////////////////////////////////////////////////////////////////////////////
1031
1032 /// The internal representation of a `Thread` handle
1033 struct Inner {
1034 name: Option<CString>, // Guaranteed to be UTF-8
1035 id: ThreadId,
1036 parker: Parker,
1037 }
1038
1039 #[derive(Clone)]
1040 #[stable(feature = "rust1", since = "1.0.0")]
1041 /// A handle to a thread.
1042 ///
1043 /// Threads are represented via the `Thread` type, which you can get in one of
1044 /// two ways:
1045 ///
1046 /// * By spawning a new thread, e.g., using the [`thread::spawn`][`spawn`]
1047 /// function, and calling [`thread`][`JoinHandle::thread`] on the
1048 /// [`JoinHandle`].
1049 /// * By requesting the current thread, using the [`thread::current`] function.
1050 ///
1051 /// The [`thread::current`] function is available even for threads not spawned
1052 /// by the APIs of this module.
1053 ///
1054 /// There is usually no need to create a `Thread` struct yourself, one
1055 /// should instead use a function like `spawn` to create new threads, see the
1056 /// docs of [`Builder`] and [`spawn`] for more details.
1057 ///
1058 /// [`thread::current`]: current
1059 pub struct Thread {
1060 inner: Arc<Inner>,
1061 }
1062
1063 impl Thread {
1064 // Used only internally to construct a thread object without spawning
1065 // Panics if the name contains nuls.
1066 pub(crate) fn new(name: Option<String>) -> Thread {
1067 let cname =
1068 name.map(|n| CString::new(n).expect("thread name may not contain interior null bytes"));
1069 Thread {
1070 inner: Arc::new(Inner { name: cname, id: ThreadId::new(), parker: Parker::new() }),
1071 }
1072 }
1073
1074 /// Atomically makes the handle's token available if it is not already.
1075 ///
1076 /// Every thread is equipped with some basic low-level blocking support, via
1077 /// the [`park`][park] function and the `unpark()` method. These can be
1078 /// used as a more CPU-efficient implementation of a spinlock.
1079 ///
1080 /// See the [park documentation][park] for more details.
1081 ///
1082 /// # Examples
1083 ///
1084 /// ```
1085 /// use std::thread;
1086 /// use std::time::Duration;
1087 ///
1088 /// let parked_thread = thread::Builder::new()
1089 /// .spawn(|| {
1090 /// println!("Parking thread");
1091 /// thread::park();
1092 /// println!("Thread unparked");
1093 /// })
1094 /// .unwrap();
1095 ///
1096 /// // Let some time pass for the thread to be spawned.
1097 /// thread::sleep(Duration::from_millis(10));
1098 ///
1099 /// println!("Unpark the thread");
1100 /// parked_thread.thread().unpark();
1101 ///
1102 /// parked_thread.join().unwrap();
1103 /// ```
1104 #[stable(feature = "rust1", since = "1.0.0")]
1105 #[inline]
1106 pub fn unpark(&self) {
1107 self.inner.parker.unpark();
1108 }
1109
1110 /// Gets the thread's unique identifier.
1111 ///
1112 /// # Examples
1113 ///
1114 /// ```
1115 /// use std::thread;
1116 ///
1117 /// let other_thread = thread::spawn(|| {
1118 /// thread::current().id()
1119 /// });
1120 ///
1121 /// let other_thread_id = other_thread.join().unwrap();
1122 /// assert!(thread::current().id() != other_thread_id);
1123 /// ```
1124 #[stable(feature = "thread_id", since = "1.19.0")]
1125 pub fn id(&self) -> ThreadId {
1126 self.inner.id
1127 }
1128
1129 /// Gets the thread's name.
1130 ///
1131 /// For more information about named threads, see
1132 /// [this module-level documentation][naming-threads].
1133 ///
1134 /// # Examples
1135 ///
1136 /// Threads by default have no name specified:
1137 ///
1138 /// ```
1139 /// use std::thread;
1140 ///
1141 /// let builder = thread::Builder::new();
1142 ///
1143 /// let handler = builder.spawn(|| {
1144 /// assert!(thread::current().name().is_none());
1145 /// }).unwrap();
1146 ///
1147 /// handler.join().unwrap();
1148 /// ```
1149 ///
1150 /// Thread with a specified name:
1151 ///
1152 /// ```
1153 /// use std::thread;
1154 ///
1155 /// let builder = thread::Builder::new()
1156 /// .name("foo".into());
1157 ///
1158 /// let handler = builder.spawn(|| {
1159 /// assert_eq!(thread::current().name(), Some("foo"))
1160 /// }).unwrap();
1161 ///
1162 /// handler.join().unwrap();
1163 /// ```
1164 ///
1165 /// [naming-threads]: ./index.html#naming-threads
1166 #[stable(feature = "rust1", since = "1.0.0")]
1167 pub fn name(&self) -> Option<&str> {
1168 self.cname().map(|s| unsafe { str::from_utf8_unchecked(s.to_bytes()) })
1169 }
1170
1171 fn cname(&self) -> Option<&CStr> {
1172 self.inner.name.as_deref()
1173 }
1174 }
1175
1176 #[stable(feature = "rust1", since = "1.0.0")]
1177 impl fmt::Debug for Thread {
1178 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1179 f.debug_struct("Thread").field("id", &self.id()).field("name", &self.name()).finish()
1180 }
1181 }
1182
1183 ////////////////////////////////////////////////////////////////////////////////
1184 // JoinHandle
1185 ////////////////////////////////////////////////////////////////////////////////
1186
1187 /// A specialized [`Result`] type for threads.
1188 ///
1189 /// Indicates the manner in which a thread exited.
1190 ///
1191 /// The value contained in the `Result::Err` variant
1192 /// is the value the thread panicked with;
1193 /// that is, the argument the `panic!` macro was called with.
1194 /// Unlike with normal errors, this value doesn't implement
1195 /// the [`Error`](crate::error::Error) trait.
1196 ///
1197 /// Thus, a sensible way to handle a thread panic is to either:
1198 ///
1199 /// 1. propagate the panic with [`std::panic::resume_unwind`]
1200 /// 2. or in case the thread is intended to be a subsystem boundary
1201 /// that is supposed to isolate system-level failures,
1202 /// match on the `Err` variant and handle the panic in an appropriate way
1203 ///
1204 /// A thread that completes without panicking is considered to exit successfully.
1205 ///
1206 /// # Examples
1207 ///
1208 /// Matching on the result of a joined thread:
1209 ///
1210 /// ```no_run
1211 /// use std::{fs, thread, panic};
1212 ///
1213 /// fn copy_in_thread() -> thread::Result<()> {
1214 /// thread::spawn(|| {
1215 /// fs::copy("foo.txt", "bar.txt").unwrap();
1216 /// }).join()
1217 /// }
1218 ///
1219 /// fn main() {
1220 /// match copy_in_thread() {
1221 /// Ok(_) => println!("copy succeeded"),
1222 /// Err(e) => panic::resume_unwind(e),
1223 /// }
1224 /// }
1225 /// ```
1226 ///
1227 /// [`Result`]: crate::result::Result
1228 /// [`std::panic::resume_unwind`]: crate::panic::resume_unwind
1229 #[stable(feature = "rust1", since = "1.0.0")]
1230 pub type Result<T> = crate::result::Result<T, Box<dyn Any + Send + 'static>>;
1231
1232 // This packet is used to communicate the return value between the child thread
1233 // and the parent thread. Memory is shared through the `Arc` within and there's
1234 // no need for a mutex here because synchronization happens with `join()` (the
1235 // parent thread never reads this packet until the child has exited).
1236 //
1237 // This packet itself is then stored into a `JoinInner` which in turns is placed
1238 // in `JoinHandle` and `JoinGuard`. Due to the usage of `UnsafeCell` we need to
1239 // manually worry about impls like Send and Sync. The type `T` should
1240 // already always be Send (otherwise the thread could not have been created) and
1241 // this type is inherently Sync because no methods take &self. Regardless,
1242 // however, we add inheriting impls for Send/Sync to this type to ensure it's
1243 // Send/Sync and that future modifications will still appropriately classify it.
1244 struct Packet<T>(Arc<UnsafeCell<Option<Result<T>>>>);
1245
1246 unsafe impl<T: Send> Send for Packet<T> {}
1247 unsafe impl<T: Sync> Sync for Packet<T> {}
1248
1249 /// Inner representation for JoinHandle
1250 struct JoinInner<T> {
1251 native: Option<imp::Thread>,
1252 thread: Thread,
1253 packet: Packet<T>,
1254 }
1255
1256 impl<T> JoinInner<T> {
1257 fn join(&mut self) -> Result<T> {
1258 self.native.take().unwrap().join();
1259 unsafe { (*self.packet.0.get()).take().unwrap() }
1260 }
1261 }
1262
1263 /// An owned permission to join on a thread (block on its termination).
1264 ///
1265 /// A `JoinHandle` *detaches* the associated thread when it is dropped, which
1266 /// means that there is no longer any handle to thread and no way to `join`
1267 /// on it.
1268 ///
1269 /// Due to platform restrictions, it is not possible to [`Clone`] this
1270 /// handle: the ability to join a thread is a uniquely-owned permission.
1271 ///
1272 /// This `struct` is created by the [`thread::spawn`] function and the
1273 /// [`thread::Builder::spawn`] method.
1274 ///
1275 /// # Examples
1276 ///
1277 /// Creation from [`thread::spawn`]:
1278 ///
1279 /// ```
1280 /// use std::thread;
1281 ///
1282 /// let join_handle: thread::JoinHandle<_> = thread::spawn(|| {
1283 /// // some work here
1284 /// });
1285 /// ```
1286 ///
1287 /// Creation from [`thread::Builder::spawn`]:
1288 ///
1289 /// ```
1290 /// use std::thread;
1291 ///
1292 /// let builder = thread::Builder::new();
1293 ///
1294 /// let join_handle: thread::JoinHandle<_> = builder.spawn(|| {
1295 /// // some work here
1296 /// }).unwrap();
1297 /// ```
1298 ///
1299 /// Child being detached and outliving its parent:
1300 ///
1301 /// ```no_run
1302 /// use std::thread;
1303 /// use std::time::Duration;
1304 ///
1305 /// let original_thread = thread::spawn(|| {
1306 /// let _detached_thread = thread::spawn(|| {
1307 /// // Here we sleep to make sure that the first thread returns before.
1308 /// thread::sleep(Duration::from_millis(10));
1309 /// // This will be called, even though the JoinHandle is dropped.
1310 /// println!("♫ Still alive ♫");
1311 /// });
1312 /// });
1313 ///
1314 /// original_thread.join().expect("The thread being joined has panicked");
1315 /// println!("Original thread is joined.");
1316 ///
1317 /// // We make sure that the new thread has time to run, before the main
1318 /// // thread returns.
1319 ///
1320 /// thread::sleep(Duration::from_millis(1000));
1321 /// ```
1322 ///
1323 /// [`thread::Builder::spawn`]: Builder::spawn
1324 /// [`thread::spawn`]: spawn
1325 #[stable(feature = "rust1", since = "1.0.0")]
1326 pub struct JoinHandle<T>(JoinInner<T>);
1327
1328 #[stable(feature = "joinhandle_impl_send_sync", since = "1.29.0")]
1329 unsafe impl<T> Send for JoinHandle<T> {}
1330 #[stable(feature = "joinhandle_impl_send_sync", since = "1.29.0")]
1331 unsafe impl<T> Sync for JoinHandle<T> {}
1332
1333 impl<T> JoinHandle<T> {
1334 /// Extracts a handle to the underlying thread.
1335 ///
1336 /// # Examples
1337 ///
1338 /// ```
1339 /// use std::thread;
1340 ///
1341 /// let builder = thread::Builder::new();
1342 ///
1343 /// let join_handle: thread::JoinHandle<_> = builder.spawn(|| {
1344 /// // some work here
1345 /// }).unwrap();
1346 ///
1347 /// let thread = join_handle.thread();
1348 /// println!("thread id: {:?}", thread.id());
1349 /// ```
1350 #[stable(feature = "rust1", since = "1.0.0")]
1351 pub fn thread(&self) -> &Thread {
1352 &self.0.thread
1353 }
1354
1355 /// Waits for the associated thread to finish.
1356 ///
1357 /// In terms of [atomic memory orderings], the completion of the associated
1358 /// thread synchronizes with this function returning. In other words, all
1359 /// operations performed by that thread are ordered before all
1360 /// operations that happen after `join` returns.
1361 ///
1362 /// If the child thread panics, [`Err`] is returned with the parameter given
1363 /// to [`panic!`].
1364 ///
1365 /// [`Err`]: crate::result::Result::Err
1366 /// [atomic memory orderings]: crate::sync::atomic
1367 ///
1368 /// # Panics
1369 ///
1370 /// This function may panic on some platforms if a thread attempts to join
1371 /// itself or otherwise may create a deadlock with joining threads.
1372 ///
1373 /// # Examples
1374 ///
1375 /// ```
1376 /// use std::thread;
1377 ///
1378 /// let builder = thread::Builder::new();
1379 ///
1380 /// let join_handle: thread::JoinHandle<_> = builder.spawn(|| {
1381 /// // some work here
1382 /// }).unwrap();
1383 /// join_handle.join().expect("Couldn't join on the associated thread");
1384 /// ```
1385 #[stable(feature = "rust1", since = "1.0.0")]
1386 pub fn join(mut self) -> Result<T> {
1387 self.0.join()
1388 }
1389 }
1390
1391 impl<T> AsInner<imp::Thread> for JoinHandle<T> {
1392 fn as_inner(&self) -> &imp::Thread {
1393 self.0.native.as_ref().unwrap()
1394 }
1395 }
1396
1397 impl<T> IntoInner<imp::Thread> for JoinHandle<T> {
1398 fn into_inner(self) -> imp::Thread {
1399 self.0.native.unwrap()
1400 }
1401 }
1402
1403 #[stable(feature = "std_debug", since = "1.16.0")]
1404 impl<T> fmt::Debug for JoinHandle<T> {
1405 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1406 f.pad("JoinHandle { .. }")
1407 }
1408 }
1409
1410 fn _assert_sync_and_send() {
1411 fn _assert_both<T: Send + Sync>() {}
1412 _assert_both::<JoinHandle<()>>();
1413 _assert_both::<Thread>();
1414 }