Skip to content

Commit 32f418f

Browse files
Rollup merge of rust-lang#41981 - gamazeps:thread-detach, r=frewsxcv
[Doc] Expands `detach` documentation in `thread::JoinHande`. Part of rust-lang#29378 . - Adds an example of a thread detaching. - Expands what `detaching` means. r? @steveklabnik
2 parents d7798c3 + b76b9e1 commit 32f418f

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

src/libstd/thread/mod.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,11 +1094,12 @@ impl<T> JoinInner<T> {
10941094

10951095
/// An owned permission to join on a thread (block on its termination).
10961096
///
1097-
/// A `JoinHandle` *detaches* the child thread when it is dropped.
1097+
/// A `JoinHandle` *detaches* the associated thread when it is dropped, which
1098+
/// means that there is no longer any handle to thread and no way to `join`
1099+
/// on it.
10981100
///
10991101
/// Due to platform restrictions, it is not possible to [`Clone`] this
1100-
/// handle: the ability to join a child thread is a uniquely-owned
1101-
/// permission.
1102+
/// handle: the ability to join a thread is a uniquely-owned permission.
11021103
///
11031104
/// This `struct` is created by the [`thread::spawn`] function and the
11041105
/// [`thread::Builder::spawn`] method.
@@ -1127,6 +1128,30 @@ impl<T> JoinInner<T> {
11271128
/// }).unwrap();
11281129
/// ```
11291130
///
1131+
/// Child being detached and outliving its parent:
1132+
///
1133+
/// ```no_run
1134+
/// use std::thread;
1135+
/// use std::time::Duration;
1136+
///
1137+
/// let original_thread = thread::spawn(|| {
1138+
/// let _detached_thread = thread::spawn(|| {
1139+
/// // Here we sleep to make sure that the first thread returns before.
1140+
/// thread::sleep(Duration::from_millis(10));
1141+
/// // This will be called, even though the JoinHandle is dropped.
1142+
/// println!("♫ Still alive ♫");
1143+
/// });
1144+
/// });
1145+
///
1146+
/// let _ = original_thread.join();
1147+
/// println!("Original thread is joined.");
1148+
///
1149+
/// // We make sure that the new thread has time to run, before the main
1150+
/// // thread returns.
1151+
///
1152+
/// thread::sleep(Duration::from_millis(1000));
1153+
/// ```
1154+
///
11301155
/// [`Clone`]: ../../std/clone/trait.Clone.html
11311156
/// [`thread::spawn`]: fn.spawn.html
11321157
/// [`thread::Builder::spawn`]: struct.Builder.html#method.spawn

0 commit comments

Comments
 (0)