Skip to content

Instantly share code, notes, and snippets.

@glommer
Created April 30, 2021 18:15
Show Gist options
  • Save glommer/4aa64061fa77d2770ac99a6753a11e12 to your computer and use it in GitHub Desktop.
Save glommer/4aa64061fa77d2770ac99a6753a11e12 to your computer and use it in GitHub Desktop.
let keep_running = Rc::new(Cell::new(true));
let kr = keep_running.clone(); // there are now two objects pointing to the same place
// the ownership of this copy is passed to the task.
let background_printer = Task::local(async move {
while kr.get() {
sleep(Duration::from_millis(100)).await;
}
});
sleep(Duration::from_seconds(2)).await;
keep_running.set(false);
background_printer.await;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment