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
Closed

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

yoshuawuyts opened this issue Oct 8, 2019 · 0 comments · Fixed by #300
Labels
api design Open design questions

Comments

@yoshuawuyts
Copy link
Contributor

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;
}
@yoshuawuyts yoshuawuyts added the api design Open design questions label Oct 8, 2019
This was referenced Oct 9, 2019
@ghost ghost closed this as completed in #300 Oct 14, 2019
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api design Open design questions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant