Skip to content

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

Closed

Description

@booleancoercion

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?

Activity

  1. jyn514 commented on Jan 3, 2021

    @jyn514
    Member

    @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.

  2. booleancoercion commented on Jan 3, 2021

    @booleancoercion
    ContributorAuthor

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

  3. jyn514 commented on Jan 3, 2021

    @jyn514
    Member

    @booleancoercion yup, that would be great :)

  4. added
    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and tools
    T-libs-api[DEPRECATED; DO NOT USE]
    C-bugCategory: This is a bug.
    on Jan 3, 2021
  5. added a commit that references this issue on Jan 5, 2021
    cbdc241
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsC-bugCategory: This is a bug.T-libs-api[DEPRECATED; DO NOT USE]

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions