0

I want to implement a Future that busy-waits on its completion.

According to this link, this is one option

impl Future for Delay {
    type Output = &'static str;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>)
        -> Poll<&'static str>
    {
        if Instant::now() >= self.when {
            println!("Hello world");
            Poll::Ready("done")
        } else {
            // Ignore this line for now.
            cx.waker().wake_by_ref();
            Poll::Pending
        }
    }
}

What I am worried about is this future starving out other tasks. Is it guaranteed that if I call wake inside the Future that returns Pending, that this wake will execute as a last item after epol, timers, and any other wakes called before?

Is this the good way to implement busy-wait Future?

Samuel Hapak
  • 6,950
  • 3
  • 35
  • 58

0 Answers0