idle_behavior/
idle_behavior.rs

1//! This sample demonstrates a thread pool with one thread per logical core and only one task
2//! spinning. Other than the one thread, the system should remain idle, demonstrating good behavior
3//! for small workloads.
4
5#![expect(clippy::print_stdout, reason = "Allowed in examples.")]
6
7use bevy_platform::time::Instant;
8use bevy_tasks::TaskPoolBuilder;
9use core::time::Duration;
10
11fn main() {
12    let pool = TaskPoolBuilder::new()
13        .thread_name("Idle Behavior ThreadPool".to_string())
14        .build();
15
16    pool.scope(|s| {
17        for i in 0..1 {
18            s.spawn(async move {
19                println!("Blocking for 10 seconds");
20                let now = Instant::now();
21                while Instant::now() - now < Duration::from_millis(10000) {
22                    // spin, simulating work being done
23                }
24
25                println!(
26                    "Thread {:?} index {} finished",
27                    std::thread::current().id(),
28                    i
29                );
30            });
31        }
32    });
33
34    println!("all tasks finished");
35}