From 30d63e0a59b2aa5ba76913434a78d6c9fb167d78 Mon Sep 17 00:00:00 2001 From: Martin Dahl Date: Wed, 1 Sep 2021 16:18:02 +0200 Subject: [PATCH] exit timer example properly --- examples/wall_timer.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/examples/wall_timer.rs b/examples/wall_timer.rs index 7808771..c977d4c 100644 --- a/examples/wall_timer.rs +++ b/examples/wall_timer.rs @@ -1,5 +1,7 @@ use futures::executor::LocalPool; use futures::task::LocalSpawnExt; +use std::rc::Rc; +use std::cell::RefCell; use r2r; async fn timer_task(mut t: r2r::Timer) -> Result<(), Box> { @@ -29,16 +31,24 @@ fn main() -> Result<(), Box> { let mut pool = LocalPool::new(); let spawner = pool.spawner(); + let is_done = Rc::new(RefCell::new(false)); + + let task_is_done = is_done.clone(); spawner.spawn_local(async move { match timer_task(timer).await { - Ok(()) => println!("exiting"), + Ok(()) => { + *task_is_done.borrow_mut() = true; + println!("exiting"); + }, Err(e) => println!("error: {}", e), } })?; - loop { + while !*is_done.borrow() { node.spin_once(std::time::Duration::from_millis(100)); pool.run_until_stalled(); } + + Ok(()) }