28
28
//! When the main thread of a Rust program terminates, the entire program shuts
29
29
//! down, even if other threads are still running. However, this module provides
30
30
//! convenient facilities for automatically waiting for the termination of a
31
- //! child thread (i.e., join).
31
+ //! thread (i.e., join).
32
32
//!
33
33
//! ## Spawning a thread
34
34
//!
42
42
//! });
43
43
//! ```
44
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 .
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 .
48
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:
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:
52
53
//!
53
54
//! ```rust
54
55
//! use std::thread;
55
56
//!
56
- //! let child = thread::spawn(move || {
57
+ //! let thread_join_handle = thread::spawn(move || {
57
58
//! // some work here
58
59
//! });
59
60
//! // some work here
60
- //! let res = child .join();
61
+ //! let res = thread_join_handle .join();
61
62
//! ```
62
63
//!
63
64
//! 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.
66
71
//!
67
72
//! ## Configuring threads
68
73
//!
69
74
//! 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:
71
76
//!
72
77
//! ```rust
73
78
//! # #![allow(unused_must_use)]
74
79
//! use std::thread;
75
80
//!
76
- //! thread::Builder::new().name("child1 ".to_string()).spawn(move || {
81
+ //! thread::Builder::new().name("thread1 ".to_string()).spawn(move || {
77
82
//! println!("Hello, world!");
78
83
//! });
79
84
//! ```
@@ -344,7 +349,7 @@ impl Builder {
344
349
/// The spawned thread may outlive the caller (unless the caller thread
345
350
/// is the main thread; the whole process is terminated when the main
346
351
/// 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.
348
353
///
349
354
/// For a more complete documentation see [`thread::spawn`][`spawn`].
350
355
///
@@ -389,7 +394,7 @@ impl Builder {
389
394
/// The spawned thread may outlive the caller (unless the caller thread
390
395
/// is the main thread; the whole process is terminated when the main
391
396
/// 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.
393
398
///
394
399
/// This method is identical to [`thread::Builder::spawn`][`Builder::spawn`],
395
400
/// except for the relaxed lifetime bounds, which render it unsafe.
@@ -516,15 +521,16 @@ impl Builder {
516
521
517
522
/// Spawns a new thread, returning a [`JoinHandle`] for it.
518
523
///
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.)
526
532
///
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
528
534
/// want to specify the stack size or the name of the thread, use this API
529
535
/// instead.
530
536
///
@@ -533,8 +539,8 @@ impl Builder {
533
539
///
534
540
/// - The `'static` constraint means that the closure and its return value
535
541
/// 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
+ ///
538
544
/// Indeed if the thread, and by extension its return value, can outlive their
539
545
/// caller, we need to make sure that they will be valid afterwards, and since
540
546
/// 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 {
1236
1242
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1237
1243
pub type Result < T > = crate :: result:: Result < T , Box < dyn Any + Send + ' static > > ;
1238
1244
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
1241
1247
// 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).
1243
1249
//
1244
1250
// This packet itself is then stored into a `JoinInner` which in turns is placed
1245
1251
// in `JoinHandle` and `JoinGuard`. Due to the usage of `UnsafeCell` we need to
@@ -1303,7 +1309,7 @@ impl<T> JoinInner<T> {
1303
1309
/// }).unwrap();
1304
1310
/// ```
1305
1311
///
1306
- /// Child being detached and outliving its parent :
1312
+ /// A thread being detached and outliving the thread that spawned it :
1307
1313
///
1308
1314
/// ```no_run
1309
1315
/// use std::thread;
@@ -1361,12 +1367,15 @@ impl<T> JoinHandle<T> {
1361
1367
1362
1368
/// Waits for the associated thread to finish.
1363
1369
///
1370
+ /// This function will return immediately if the associated thread has already finished.
1371
+ ///
1364
1372
/// In terms of [atomic memory orderings], the completion of the associated
1365
1373
/// 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
1367
1376
/// operations that happen after `join` returns.
1368
1377
///
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
1370
1379
/// to [`panic!`].
1371
1380
///
1372
1381
/// [`Err`]: crate::result::Result::Err
0 commit comments