.wait() never wakes up
jobafr opened this issue · comments
jobafr commented
Hi,
the documentation for OnceCell::wait()
says: "Gets the reference to the underlying value, blocking the current thread until it is set."
If I understand correctly, this should make it possible to use it as a single-shot broadcast channel. However, it seems the method never returns, even after .set
has been called by another thread.
Minimal example:
use once_cell; // 1.18.0
const SINGLE_SHOT : once_cell::sync::OnceCell<()> = once_cell::sync::OnceCell::new();
fn main() {
let h = std::thread::spawn(|| {
SINGLE_SHOT.wait();
println!("got it");
});
std::thread::sleep_ms(200);
println!("setting....");
SINGLE_SHOT.set(()).unwrap();
println!("value is now set, waiting for the other thread");
h.join().unwrap();
println!("joined");
}
Expected output:
setting....
value is now set, waiting for the other thread
got it
joined
Actual output:
setting....
value is now set, waiting for the other thread
Is this a bug or am I doing something wrong?
Alex Kladov commented
const SINGLE_SHOT :
You want static there
jobafr commented
You're right, I do. Thanks :)