Consider the following code
extern crate futures;
use std::sync::{atomic, Arc};
use futures::*;
struct F(Arc<atomic::AtomicBool>);
impl Future for F {
type Item = ();
type Error = ();
fn poll(&mut self) -> Result<Async<Self::Item>, Self::Error> {
println!("Check if flag is set");
if self.0.load(atomic::Ordering::Relaxed) {
Ok(Async::Ready(()))
} else {
Ok(Async::NotReady)
}
}
}
fn main() {
let flag = Arc::new(atomic::AtomicBool::new(false));
let future = F(flag.clone());
::std::thread::spawn(move || {
::std::thread::sleep_ms(10);
println!("set flag");
flag.store(true, atomic::Ordering::Relaxed);
});
let result = future.wait();
println!("result: {:?}", result);
}
The created thread sets the flag that awaits the future. We also slept spawned thread, so the initial call .poll()from .wait()before setting the flag. This makes .wait()blocking (apparently) endlessly. If we uncomment another thread::sleep_ms, .wait()returns and outputs the result ( ()).
I would expect the current thread to try to decide the future by calling pollseveral times, since we are blocking the current thread. However, this does not happen.
, , , park ed NotReady poll . , , .
?