Skip to content

Commit

Permalink
feat: convert busy loop to thread::sleep
Browse files Browse the repository at this point in the history
  • Loading branch information
BD103 committed Feb 13, 2024
1 parent dda277f commit 74b1f1f
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions examples/async_tasks/async_compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use bevy::{
tasks::{block_on, futures_lite::future, AsyncComputeTaskPool, Task},
};
use rand::Rng;
use std::time::{Duration, Instant};
use std::{thread, time::Duration};

fn main() {
App::new()
Expand Down Expand Up @@ -60,13 +60,11 @@ fn spawn_tasks(mut commands: Commands) {
let entity = commands.spawn_empty().id();
let task = thread_pool.spawn(async move {
let mut rng = rand::thread_rng();
let start_time = Instant::now();

let duration = Duration::from_secs_f32(rng.gen_range(0.05..0.2));
while start_time.elapsed() < duration {
// Spinning for 'duration', simulating doing hard
// compute work generating translation coords!
std::hint::spin_loop();
}

// Pretend this is a time-intensive function. :)
thread::sleep(duration);

// Such hard work, all done!
let transform = Transform::from_xyz(x as f32, y as f32, z as f32);
Expand Down

0 comments on commit 74b1f1f

Please sign in to comment.