Skip to content

Incorrect example in documentation for std::hint::spin_loop #80644

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

Closed
booleancoercion opened this issue Jan 3, 2021 · 3 comments · Fixed by #80656
Closed

Incorrect example in documentation for std::hint::spin_loop #80644

booleancoercion opened this issue Jan 3, 2021 · 3 comments · Fixed by #80656
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools C-bug Category: This is a bug. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@booleancoercion
Copy link
Contributor

Hi, I was reading the documentation for std::hint::spin_loop when I stumbled upon this example (using the form with no hidden items):

#![allow(unused)]
fn main() { fn _inner() -> Result<(), impl core::fmt::Debug> {use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::{hint, thread};

// A shared atomic value that threads will use to coordinate
let live = Arc::new(AtomicBool::new(false));

// In a background thread we'll eventually set the value
let bg_work = {
    let live = live.clone();
    thread::spawn(move || {
        // Do some work, then make the value live
        do_some_work();
        live.store(true, Ordering::Release);
    })
};

// Back on our current thread, we wait for the value to be set
while live.load(Ordering::Acquire) {
    // The spin loop is a hint to the CPU that we're waiting, but probably
    // not for very long
    hint::spin_loop();
}

// The value is now set
fn do_some_work() {}
do_some_work();
bg_work.join()?;
Ok::<(), Box<dyn core::any::Any + Send + 'static>>(())}
_inner().unwrap() }

I think that the values for AtomicBool might be inverted, because this program never terminates when run in the playground. Should this be fixed or is there something I'm missing?

@jyn514
Copy link
Member

jyn514 commented Jan 3, 2021

@booleancoercion good catch, thanks! Would you like to fix it yourself? There are instructions at https://rustc-dev-guide.rust-lang.org/getting-started.html; the command to test your changes is x.py test --stage 0 --doc library/std.

@booleancoercion
Copy link
Contributor Author

@jyn514 I'd love to! Do I just make a PR that references this issue then?

@jyn514
Copy link
Member

jyn514 commented Jan 3, 2021

@booleancoercion yup, that would be great :)

@camelid camelid added A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. C-bug Category: This is a bug. labels Jan 3, 2021
@bors bors closed this as completed in cbdc241 Jan 5, 2021
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 C-bug Category: This is a bug. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants