Skip to content

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

Closed
@booleancoercion

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?

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-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions