Skip to content
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

std::thread::yield_now() example is lacking. #134723

Open
dev-ardi opened this issue Dec 24, 2024 · 3 comments
Open

std::thread::yield_now() example is lacking. #134723

dev-ardi opened this issue Dec 24, 2024 · 3 comments
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@dev-ardi
Copy link
Contributor

dev-ardi commented Dec 24, 2024

Location

rust/library/std/src/thread/mod.rs:755

/// # Examples
///
/// ```
/// use std::thread;
///
/// thread::yield_now();
/// ```

Summary

It's not great, ideally it should show a mostly real use case.

It should maybe hint that this should not be used directly to create spin loops without first considering std::hint::spin_loop

@dev-ardi dev-ardi added the A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools label Dec 24, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 24, 2024
@Noratrieb
Copy link
Member

It should not be used to create spin loops, no one should ever create spinloops when an OS scheduler is present

@dev-ardi
Copy link
Contributor Author

dev-ardi commented Dec 24, 2024

no one should ever create spinloops when an OS scheduler is present

You could consider a CAS loop to be a spin loop. Also I don't think that that's true, an approach to this is to spin lock with spin_lock() and after a set number of iterations use yield_now to avoid being set to higher prio.

Spin locks are used sometimes in userland code in some apps like game engines.

@jieyouxu jieyouxu added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Dec 24, 2024
@the8472
Copy link
Member

the8472 commented Dec 25, 2024

You might be thinking of hint::spin_loop? CAS loops are waiting on a memory location, so they can use futexes to tell the kernel what they're waiting on and don't need thread::yield_now for that and use spin_loop to briefly wait in userspace.

to avoid being set to higher prio

Does waitonaddress do priority inheritance? On linux futexes can opt in/out of PI.

Anyway, even if that's an issue it seems like a highly specific reason that doesn't seem appropriate for a code example.

We do have uses of yield_now in std too, but that's often a "might as well try before we give up" situation, not good code.

/// A simple retry loop that keeps running `f` while it fails with the given
/// error code or until `MAX_RETRIES` is reached.
fn retry<T: PartialEq>(
mut f: impl FnMut() -> Result<T, WinError>,
ignore: WinError,
) -> Result<T, WinError> {
let mut i = MAX_RETRIES;
loop {
i -= 1;
if i == 0 {
return f();
} else {
let result = f();
if result != Err(ignore) {
return result;
}
}
thread::yield_now();
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants