]> git.lizzy.rs Git - rust.git/commitdiff
Better example for `thread::unpark`.
authorFelix Raimundo <felix.raimundo@tweag.io>
Sun, 7 May 2017 14:01:47 +0000 (16:01 +0200)
committerFelix Raimundo <felix.raimundo@tweag.io>
Sun, 7 May 2017 14:01:47 +0000 (16:01 +0200)
Part of #29378

src/libstd/thread/mod.rs

index bdf7b1134bfe7f2f9000a01368a78398b2964bda..94a2c91858ea7fd1989e96affb15112b0577f191 100644 (file)
@@ -769,7 +769,7 @@ pub(crate) fn new(name: Option<String>) -> 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 @@ pub(crate) fn new(name: Option<String>) -> 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