-
Notifications
You must be signed in to change notification settings - Fork 13.5k
expand thread::park explanation #56157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -806,9 +806,13 @@ const NOTIFIED: usize = 2; | |
/// 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In general since this is a public API, I would prefer wording to not be in terms of how the implementation could be (i.e. implemented to return immediately), but what the implementation is allowed to do (return spuriously without necessarily synchronizing with anything). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think examples are very useful to demonstrate where spurious wakeups may come from. For me personally, I have seen this "spurious wakeups allowed" stuff often, but only when I recently read "NOPs are a valid implementation" then it clicked for me. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have changed the order a bit, putting the API contract first and the example second. What do you think? |
||
/// 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: | ||
|
@@ -823,21 +827,33 @@ const NOTIFIED: usize = 2; | |
/// | ||
/// ``` | ||
/// use std::thread; | ||
/// use std::sync::{Arc, atomic::{Ordering, AtomicBool}}; | ||
/// use std::time::Duration; | ||
/// | ||
/// let parked_thread = thread::Builder::new() | ||
/// .spawn(|| { | ||
/// let flag = Arc::new(AtomicBool::new(false)); | ||
/// let flag2 = Arc::clone(&flag); | ||
/// | ||
/// let parked_thread = thread::spawn(move || { | ||
/// // We want to wait until the flag is set. We *could* just spin, but using | ||
/// // park/unpark is more efficient. | ||
/// while !flag2.load(Ordering::Acquire) { | ||
/// println!("Parking thread"); | ||
/// thread::park(); | ||
/// // We *could* get here spuriously, i.e., way before the 10ms below are over! | ||
/// // But that is no problem, we are in a loop until the flag is set anyway. | ||
/// println!("Thread unparked"); | ||
/// }) | ||
/// .unwrap(); | ||
/// } | ||
/// println!("Flag received"); | ||
/// }); | ||
/// | ||
/// // Let some time pass for the thread to be spawned. | ||
/// thread::sleep(Duration::from_millis(10)); | ||
/// | ||
/// // Set the flag, and let the thread wake up. | ||
/// // There is no race condition here, if `unpark` | ||
/// // happens first, `park` will return immediately. | ||
/// // Hence there is no risk of a deadlock. | ||
/// flag.store(true, Ordering::Release); | ||
/// println!("Unpark the thread"); | ||
/// parked_thread.thread().unpark(); | ||
/// | ||
|
Uh oh!
There was an error while loading. Please reload this page.