diff options
Diffstat (limited to 'examples/std/src/bin/tick_cancel.rs')
| -rw-r--r-- | examples/std/src/bin/tick_cancel.rs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/examples/std/src/bin/tick_cancel.rs b/examples/std/src/bin/tick_cancel.rs new file mode 100644 index 000000000..54e44790f --- /dev/null +++ b/examples/std/src/bin/tick_cancel.rs | |||
| @@ -0,0 +1,47 @@ | |||
| 1 | use std::sync::atomic::{AtomicBool, Ordering}; | ||
| 2 | use std::thread; | ||
| 3 | use std::time::Duration; | ||
| 4 | |||
| 5 | use embassy_executor::Executor; | ||
| 6 | use embassy_time::Timer; | ||
| 7 | use log::*; | ||
| 8 | use static_cell::StaticCell; | ||
| 9 | |||
| 10 | #[embassy_executor::task] | ||
| 11 | async fn run() { | ||
| 12 | loop { | ||
| 13 | info!("tick"); | ||
| 14 | Timer::after_secs(1).await; | ||
| 15 | } | ||
| 16 | } | ||
| 17 | |||
| 18 | static DONE: StaticCell<AtomicBool> = StaticCell::new(); | ||
| 19 | static EXECUTOR: StaticCell<Executor> = StaticCell::new(); | ||
| 20 | |||
| 21 | fn main() { | ||
| 22 | env_logger::builder() | ||
| 23 | .filter_level(log::LevelFilter::Debug) | ||
| 24 | .format_timestamp_nanos() | ||
| 25 | .init(); | ||
| 26 | |||
| 27 | let done = DONE.init(AtomicBool::new(false)); | ||
| 28 | let done_cb = || done.load(Ordering::Relaxed); | ||
| 29 | |||
| 30 | let server_thread = thread::spawn(move || { | ||
| 31 | let executor = EXECUTOR.init(Executor::new()); | ||
| 32 | executor.run_until( | ||
| 33 | |spawner| { | ||
| 34 | spawner.spawn(run().unwrap()); | ||
| 35 | }, | ||
| 36 | done_cb, | ||
| 37 | ); | ||
| 38 | info!("Executor finished"); | ||
| 39 | }); | ||
| 40 | |||
| 41 | thread::sleep(Duration::from_secs(5)); | ||
| 42 | |||
| 43 | info!("Cancelling executor"); | ||
| 44 | done.store(true, Ordering::Relaxed); | ||
| 45 | |||
| 46 | server_thread.join().unwrap(); | ||
| 47 | } | ||
