Closed
Description
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?