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

Use std::thread::sleep instead of spin-waiting in the async_compute example #11856

Merged
merged 2 commits into from
Feb 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 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,12 +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!
}

// 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