Does async sleep/delay require a separate thread?

Mini-tokio example: website/ at master · tokio-rs/website · GitHub

It seems awfully expensive to spin up a thread just for a timer.

Is there a way to do timers / delay / sleep in async without spinning up another thread ?

In theory, the executor could handle this specially. If it has a priority queue of alarm times, it can use a timeout wait function when it pauses instead of an unbounded one. Then, your delay future just needs to know how to register such an alarm, as it's specific to the executor.

In the code you linked to, for example, you'd replace recv on line 107 with recv_deadline or recv_timeout.

Do you know of any doing this in practice?

Also, once we're in async, 'sleep' means 'sleep_atleast' rather than 'sleep_precisely', since, the moment one coroutine gets swapped out, there is no guarantee when it gets swapped back in. In such a world, it seems there is lots of "leeway" with implementing when coroutines are woke.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.