Skip to content

Commit 5573c47

Browse files
author
Felix Raimundo
committed
Better example for thread::unpark.
Part of #29378
1 parent d9628f9 commit 5573c47

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/libstd/thread/mod.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ impl Thread {
769769
/// Atomically makes the handle's token available if it is not already.
770770
///
771771
/// Every thread is equipped with some basic low-level blocking support, via
772-
/// the [`park()`][park] function and the `unpark` method. These can be
772+
/// the [`park()`][park] function and the `unpark()` method. These can be
773773
/// used as a more CPU-efficient implementation of a spinlock.
774774
///
775775
/// See the [module doc][thread] for more detail.
@@ -779,14 +779,21 @@ impl Thread {
779779
/// ```
780780
/// use std::thread;
781781
///
782-
/// let handler = thread::Builder::new()
782+
/// let parked_thread = thread::Builder::new()
783783
/// .spawn(|| {
784-
/// let thread = thread::current();
785-
/// thread.unpark();
784+
/// println!("Parking thread");
785+
/// thread::park();
786+
/// println!("Thread unparked");
786787
/// })
787788
/// .unwrap();
788789
///
789-
/// handler.join().unwrap();
790+
/// // Let some time pass for the thread to be spawned.
791+
/// thread::sleep(Duration::from_millis(10));
792+
///
793+
/// println!("Unpark the thread");
794+
/// parked_thread.thread().unpark();
795+
///
796+
/// parked_thread.join().unwrap();
790797
/// ```
791798
///
792799
/// [thread]: index.html

0 commit comments

Comments
 (0)