Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[discussion] Add task::yield_now()? #290

Closed
yoshuawuyts opened this issue Oct 8, 2019 · 0 comments · Fixed by #300

Comments

@yoshuawuyts
Copy link
Member

commented Oct 8, 2019

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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.