]> git.lizzy.rs Git - rust.git/commitdiff
expand thread::park explanation
authorRalf Jung <post@ralfj.de>
Thu, 22 Nov 2018 09:54:04 +0000 (10:54 +0100)
committerRalf Jung <post@ralfj.de>
Thu, 22 Nov 2018 09:54:04 +0000 (10:54 +0100)
src/libstd/thread/mod.rs

index 8a845efd41362ec66da7bb0210889b03bd1f672c..99f8fa390d227eb4c8dba5ffd4b8292fc7897731 100644 (file)
@@ -806,9 +806,13 @@ pub fn sleep(dur: Duration) {
 /// In other words, each [`Thread`] acts a bit like a spinlock that can be
 /// locked and unlocked using `park` and `unpark`.
 ///
+/// Notice that it would be a valid (but inefficient) implementation to make both [`park`] and
+/// [`unpark`] NOPs that return immediately.  Being unblocked does not imply
+/// any synchronization with someone that unparked this thread, it could also be spurious.
+///
 /// The API is typically used by acquiring a handle to the current thread,
 /// placing that handle in a shared data structure so that other threads can
-/// find it, and then `park`ing. When some desired condition is met, another
+/// find it, and then `park`ing in a loop. When some desired condition is met, another
 /// thread calls [`unpark`] on the handle.
 ///
 /// The motivation for this design is twofold:
@@ -829,6 +833,7 @@ pub fn sleep(dur: Duration) {
 ///     .spawn(|| {
 ///         println!("Parking thread");
 ///         thread::park();
+///         // We *could* get here spuriously, i.e., way before the 10ms below are over!
 ///         println!("Thread unparked");
 ///     })
 ///     .unwrap();