basic/basic.rs
1use std::{thread, time::Duration};
2
3fn on_timeout() {
4 println!("timeout!");
5}
6
7fn main() {
8 let cancel = spawn_timeout::spawn_timeout(&on_timeout, Duration::from_secs(3));
9 let _ = spawn_timeout::spawn_timeout(&on_timeout, Duration::from_secs(3));
10
11 // Waiting before cancelling this instance of spawn_timeout.
12 thread::sleep(Duration::from_millis(1500));
13
14 cancel();
15
16 println!("The first instance of spawn_timeout has been succesfully stopped");
17
18 // Sleeping for a long time for the sake of this example.
19 thread::sleep(Duration::from_millis(u64::MAX));
20}