exit timer example properly

This commit is contained in:
Martin Dahl 2021-09-01 16:18:02 +02:00
parent 585e41a6a0
commit 30d63e0a59
1 changed files with 12 additions and 2 deletions

View File

@ -1,5 +1,7 @@
use futures::executor::LocalPool; use futures::executor::LocalPool;
use futures::task::LocalSpawnExt; use futures::task::LocalSpawnExt;
use std::rc::Rc;
use std::cell::RefCell;
use r2r; use r2r;
async fn timer_task(mut t: r2r::Timer) -> Result<(), Box<dyn std::error::Error>> { async fn timer_task(mut t: r2r::Timer) -> Result<(), Box<dyn std::error::Error>> {
@ -29,16 +31,24 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut pool = LocalPool::new(); let mut pool = LocalPool::new();
let spawner = pool.spawner(); let spawner = pool.spawner();
let is_done = Rc::new(RefCell::new(false));
let task_is_done = is_done.clone();
spawner.spawn_local(async move { spawner.spawn_local(async move {
match timer_task(timer).await { match timer_task(timer).await {
Ok(()) => println!("exiting"), Ok(()) => {
*task_is_done.borrow_mut() = true;
println!("exiting");
},
Err(e) => println!("error: {}", e), Err(e) => println!("error: {}", e),
} }
})?; })?;
loop { while !*is_done.borrow() {
node.spin_once(std::time::Duration::from_millis(100)); node.spin_once(std::time::Duration::from_millis(100));
pool.run_until_stalled(); pool.run_until_stalled();
} }
Ok(())
} }