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

One less thread available than expected in TaskPool Resource. #416

Closed
GrantMoyer opened this issue Sep 2, 2020 · 3 comments
Closed

One less thread available than expected in TaskPool Resource. #416

GrantMoyer opened this issue Sep 2, 2020 · 3 comments
Labels
C-Bug An unexpected or incorrect behavior

Comments

@GrantMoyer
Copy link
Contributor

GrantMoyer commented Sep 2, 2020

Even though ComputeTaskPool::thread_num() reports 4, apparently only 3 are used for work when accessing the ComputeTaskPool resource. Maybe one of the threads in the Resource is inadvertently blocked.

Affects master branch. Does not affect AsyncComputeTaskPool resource or IOTaskPool resource.

Code to reproduce:

use bevy::{
    prelude::*,
    tasks::{prelude::*, TaskPoolBuilder},
};
use std::{
    io::{stdout, Write},
    thread,
    time::{Duration, Instant},
};

fn task_system(pool: Res<ComputeTaskPool>) {
    let t0 = Instant::now();
    println!("{} threads", pool.thread_num());
    pool.scope(|s| {
        for i in 0..8 {
            s.spawn(async move {
                print!("{}", i);
                stdout().flush().unwrap();
                thread::sleep(Duration::from_secs(1));
            })
        }
    });
    let t1 = Instant::now();
    println!("\nTook {:.3}s", (t1 - t0).as_secs_f32());
}

fn main() {
    let pool = ComputeTaskPool(TaskPoolBuilder::new().num_threads(4).build());
    let t0 = Instant::now();
    println!("{} threads", pool.thread_num());
    pool.scope(|s| {
        for i in 0..8 {
            s.spawn(async move {
                print!("{}", i);
                stdout().flush().unwrap();
                thread::sleep(Duration::from_secs(1));
            })
        }
    });
    let t1 = Instant::now();
    println!("\nTook {:.3}s", (t1 - t0).as_secs_f32());

    App::build().add_startup_system(task_system.system()).run();
}

Output:

4 threads
17405326
Took 2.001s
4 threads
02631745
Took 3.001s

Expected output:

4 threads
17405326
Took 2.001s
4 threads
02631745
Took 2.001s
@GrantMoyer
Copy link
Contributor Author

Comment from @aclysma on discord:

that's expected right now, only 3 threads are available for your work because one of those threads is blocked by parallel_executor doing a blocking wait on a channel

So it looks like this is caused by #405.

@Moxinilian Moxinilian added the C-Bug An unexpected or incorrect behavior label Sep 2, 2020
@Cupnfish
Copy link
Contributor

Now run your code on my computer, the output is this, I think this bug is solved.

4 threads
02475613
Took 2.009s
6 threads
01475623
Took 2.014s

@GrantMoyer
Copy link
Contributor Author

Fixed by #437.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-Bug An unexpected or incorrect behavior
Projects
None yet
Development

No branches or pull requests

3 participants