Better example for thread::unpark.

Part of #29378
This commit is contained in:
Felix Raimundo 2017-05-07 16:01:47 +02:00
parent d9628f9389
commit 5573c4709c

View File

@ -769,7 +769,7 @@ impl Thread {
/// Atomically makes the handle's token available if it is not already.
///
/// Every thread is equipped with some basic low-level blocking support, via
/// the [`park()`][park] function and the `unpark` method. These can be
/// the [`park()`][park] function and the `unpark()` method. These can be
/// used as a more CPU-efficient implementation of a spinlock.
///
/// See the [module doc][thread] for more detail.
@ -779,14 +779,21 @@ impl Thread {
/// ```
/// use std::thread;
///
/// let handler = thread::Builder::new()
/// let parked_thread = thread::Builder::new()
/// .spawn(|| {
/// let thread = thread::current();
/// thread.unpark();
/// println!("Parking thread");
/// thread::park();
/// println!("Thread unparked");
/// })
/// .unwrap();
///
/// handler.join().unwrap();
/// // Let some time pass for the thread to be spawned.
/// thread::sleep(Duration::from_millis(10));
///
/// println!("Unpark the thread");
/// parked_thread.thread().unpark();
///
/// parked_thread.join().unwrap();
/// ```
///
/// [thread]: index.html