Path: blob/main/crates/bevy_tasks/examples/busy_behavior.rs
6604 views
//! This sample demonstrates creating a thread pool with 4 tasks and spawning 40 tasks that spin1//! for 100ms. It's expected to take about a second to run (assuming the machine has >= 4 logical2//! cores)34#![expect(clippy::print_stdout, reason = "Allowed in examples.")]56use bevy_platform::time::Instant;7use bevy_tasks::TaskPoolBuilder;8use core::time::Duration;910fn main() {11let pool = TaskPoolBuilder::new()12.thread_name("Busy Behavior ThreadPool".to_string())13.num_threads(4)14.build();1516let t0 = Instant::now();17pool.scope(|s| {18for i in 0..40 {19s.spawn(async move {20let now = Instant::now();21while Instant::now() - now < Duration::from_millis(100) {22// spin, simulating work being done23}2425println!(26"Thread {:?} index {} finished",27std::thread::current().id(),28i29);30});31}32});3334let t1 = Instant::now();35println!("all tasks finished in {} secs", (t1 - t0).as_secs_f32());36}373839