Skip to content

What is the meaning of yield_now in multi-thread runtime? #5663

Answered by Darksonn
Fancyflame asked this question in Q&A
Discussion options

You must be logged in to vote

The main function runs inside a block_on call, which means that it runs outside of the runtime's thread pool. This means that the tokio::spawn calls are not executed on the same thread as the test method, so yielding in the test method does not affect the execution of your spawned tasks.

Try executing your test inside the runtime threadpool instead:

#[tokio::main]
async fn main() {
    tokio::spawn(async move {
        let mut succeed = 0;
        let mut failed = 0;
        for _ in 0..10000 {
            if test().await {
                succeed += 1;
            } else {
                failed += 1;
            }
        }
    
        println!("testing done. succeed {succeed}, failed …

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@Fancyflame
Comment options

@Darksonn
Comment options

Answer selected by Fancyflame
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants