Description
Motivation
Sometimes it can be useful to break up longer execution into smaller parts. As a general rule task::blocking
fills that role for us (#251). But std has another method for smoothing out load within threads: thread::yield_now
. This function hints to the OS-scheduler that it's ready to yield for a while so other work can resume, improving tail-latency.
When dealing with longer-running tasks, especially with task::blocking
, it can make sense to occasionally pro-actively yield control back to schedulers. This can help with throughput, especially if there we're hitting upper limits on system resources (e.g. CPU, threads).
Implementation
I'm imagining inside task::blocking
we'd wrap thread::yield_now
inside a future to let other threads run on-cpu instead.
Inside task::spawn
/ task::block_on
this would re-schedule the current task on the futures executor. In general this seems less important than the task::blocking
use, but it might still come in useful, and overall it seems good to still let it do more or less what you'd expect it to.
The signature would be:
async fn yield_now();
Example
async fn main() {
let a = task::spawn(async { println!("hello world"); });
task::yield_now();
a.await;
}