Skip to content

Commit 2d46ae7

Browse files
committed
expand thread::park explanation
1 parent 4bec59c commit 2d46ae7

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/libstd/thread/mod.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -806,9 +806,13 @@ const NOTIFIED: usize = 2;
806806
/// In other words, each [`Thread`] acts a bit like a spinlock that can be
807807
/// locked and unlocked using `park` and `unpark`.
808808
///
809+
/// Notice that it would be a valid (but inefficient) implementation to make both [`park`] and
810+
/// [`unpark`] NOPs that return immediately. Being unblocked does not imply
811+
/// any synchronization with someone that unparked this thread, it could also be spurious.
812+
///
809813
/// The API is typically used by acquiring a handle to the current thread,
810814
/// placing that handle in a shared data structure so that other threads can
811-
/// find it, and then `park`ing. When some desired condition is met, another
815+
/// find it, and then `park`ing in a loop. When some desired condition is met, another
812816
/// thread calls [`unpark`] on the handle.
813817
///
814818
/// The motivation for this design is twofold:
@@ -829,6 +833,7 @@ const NOTIFIED: usize = 2;
829833
/// .spawn(|| {
830834
/// println!("Parking thread");
831835
/// thread::park();
836+
/// // We *could* get here spuriously, i.e., way before the 10ms below are over!
832837
/// println!("Thread unparked");
833838
/// })
834839
/// .unwrap();

0 commit comments

Comments
 (0)