2828//! When the main thread of a Rust program terminates, the entire program shuts
2929//! down, even if other threads are still running. However, this module provides
3030//! convenient facilities for automatically waiting for the termination of a
31- //! child thread (i.e., join).
31+ //! thread (i.e., join).
3232//!
3333//! ## Spawning a thread
3434//!
4242//! });
4343//! ```
4444//!
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 .
45+ //! In this example, the spawned thread is "detached," which means that there is
46+ //! no way for the program to learn when the spawned thread completes or otherwise
47+ //! terminates .
4848//!
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:
49+ //! To learn when a thread completes, it is necessary to capture the [`JoinHandle`]
50+ //! object that is returned by the call to [`spawn`], which provides
51+ //! a `join` method that allows the caller to wait for the completion of the
52+ //! spawned thread:
5253//!
5354//! ```rust
5455//! use std::thread;
5556//!
56- //! let child = thread::spawn(move || {
57+ //! let thread_join_handle = thread::spawn(move || {
5758//! // some work here
5859//! });
5960//! // some work here
60- //! let res = child .join();
61+ //! let res = thread_join_handle .join();
6162//! ```
6263//!
6364//! 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.
65+ //! value produced by the spawned thread, or [`Err`] of the value given to
66+ //! a call to [`panic!`] if the thread panicked.
67+ //!
68+ //! Note that there is no parent/child relationship between a thread that spawns a
69+ //! new thread and the thread being spawned. In particular, the spawned thread may or
70+ //! may not outlive the spawning thread, unless the spawning thread is the main thread.
6671//!
6772//! ## Configuring threads
6873//!
6974//! 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:
75+ //! which currently allows you to set the name and stack size for the thread:
7176//!
7277//! ```rust
7378//! # #![allow(unused_must_use)]
7479//! use std::thread;
7580//!
76- //! thread::Builder::new().name("child1 ".to_string()).spawn(move || {
81+ //! thread::Builder::new().name("thread1 ".to_string()).spawn(move || {
7782//! println!("Hello, world!");
7883//! });
7984//! ```
@@ -344,7 +349,7 @@ impl Builder {
344349 /// The spawned thread may outlive the caller (unless the caller thread
345350 /// is the main thread; the whole process is terminated when the main
346351 /// thread finishes). The join handle can be used to block on
347- /// termination of the child thread, including recovering its panics.
352+ /// termination of the spawned thread, including recovering its panics.
348353 ///
349354 /// For a more complete documentation see [`thread::spawn`][`spawn`].
350355 ///
@@ -389,7 +394,7 @@ impl Builder {
389394 /// The spawned thread may outlive the caller (unless the caller thread
390395 /// is the main thread; the whole process is terminated when the main
391396 /// thread finishes). The join handle can be used to block on
392- /// termination of the child thread, including recovering its panics.
397+ /// termination of the spawned thread, including recovering its panics.
393398 ///
394399 /// This method is identical to [`thread::Builder::spawn`][`Builder::spawn`],
395400 /// except for the relaxed lifetime bounds, which render it unsafe.
@@ -516,15 +521,16 @@ impl Builder {
516521
517522/// Spawns a new thread, returning a [`JoinHandle`] for it.
518523///
519- /// The join handle will implicitly *detach* the child thread upon being
520- /// dropped. In this case, the child thread may outlive the parent (unless
521- /// the parent thread is the main thread; the whole process is terminated when
522- /// the main thread finishes). Additionally, the join handle provides a [`join`]
523- /// method that can be used to join the child thread. If the child thread
524- /// panics, [`join`] will return an [`Err`] containing the argument given to
525- /// [`panic!`].
524+ /// The join handle provides a [`join`] method that can be used to join the spawned
525+ /// thread. If the spawned thread panics, [`join`] will return an [`Err`] containing
526+ /// the argument given to [`panic!`].
527+ ///
528+ /// If the join handle is dropped, the spawned thread will implicitly be *detached*.
529+ /// In this case, the spawned thread may no longer be joined.
530+ /// (It is the responsibility of the program to either eventually join threads it
531+ /// creates or detach them; otherwise, a resource leak will result.)
526532///
527- /// This will create a thread using default parameters of [`Builder`], if you
533+ /// This call will create a thread using default parameters of [`Builder`], if you
528534/// want to specify the stack size or the name of the thread, use this API
529535/// instead.
530536///
@@ -533,8 +539,8 @@ impl Builder {
533539///
534540/// - The `'static` constraint means that the closure and its return value
535541/// must have a lifetime of the whole program execution. The reason for this
536- /// is that threads can `detach` and outlive the lifetime they have been
537- /// created in.
542+ /// is that threads can outlive the lifetime they have been created in.
543+ ///
538544/// Indeed if the thread, and by extension its return value, can outlive their
539545/// caller, we need to make sure that they will be valid afterwards, and since
540546/// we *can't* know when it will return we need to have them valid as long as
@@ -1236,10 +1242,10 @@ impl fmt::Debug for Thread {
12361242#[ stable( feature = "rust1" , since = "1.0.0" ) ]
12371243pub type Result < T > = crate :: result:: Result < T , Box < dyn Any + Send + ' static > > ;
12381244
1239- // This packet is used to communicate the return value between the child thread
1240- // and the parent thread . Memory is shared through the `Arc` within and there's
1245+ // This packet is used to communicate the return value between the spawned thread
1246+ // and the rest of the program . Memory is shared through the `Arc` within and there's
12411247// no need for a mutex here because synchronization happens with `join()` (the
1242- // parent thread never reads this packet until the child has exited).
1248+ // caller will never read this packet until the thread has exited).
12431249//
12441250// This packet itself is then stored into a `JoinInner` which in turns is placed
12451251// in `JoinHandle` and `JoinGuard`. Due to the usage of `UnsafeCell` we need to
@@ -1303,7 +1309,7 @@ impl<T> JoinInner<T> {
13031309/// }).unwrap();
13041310/// ```
13051311///
1306- /// Child being detached and outliving its parent :
1312+ /// A thread being detached and outliving the thread that spawned it :
13071313///
13081314/// ```no_run
13091315/// use std::thread;
@@ -1361,12 +1367,15 @@ impl<T> JoinHandle<T> {
13611367
13621368 /// Waits for the associated thread to finish.
13631369 ///
1370+ /// This function will return immediately if the associated thread has already finished.
1371+ ///
13641372 /// In terms of [atomic memory orderings], the completion of the associated
13651373 /// thread synchronizes with this function returning. In other words, all
1366- /// operations performed by that thread are ordered before all
1374+ /// operations performed by that thread [happen
1375+ /// before](https://doc.rust-lang.org/nomicon/atomics.html#data-accesses) all
13671376 /// operations that happen after `join` returns.
13681377 ///
1369- /// If the child thread panics, [`Err`] is returned with the parameter given
1378+ /// If the associated thread panics, [`Err`] is returned with the parameter given
13701379 /// to [`panic!`].
13711380 ///
13721381 /// [`Err`]: crate::result::Result::Err
0 commit comments