Function spawn_timeout

Source
pub fn spawn_timeout<F>(
    cb: &'static F,
    wait_time: Duration,
) -> Box<dyn Fn() + Send + 'static>
where F: Fn() + Sync,
Examples found in repository?
examples/basic.rs (line 8)
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}
More examples
Hide additional examples
examples/inner_function.rs (line 12)
3fn main() {
4    let on_timeout = || {
5        println!("timeout!");
6    };
7
8    // Leaking this inner function to make its lifetime as 'static.
9    // https://doc.rust-lang.org/std/boxed/struct.Box.html#method.leak
10    let static_on_timeout = Box::leak(Box::new(on_timeout));
11
12    let cancel = spawn_timeout::spawn_timeout(static_on_timeout, Duration::from_secs(3));
13    let _ = spawn_timeout::spawn_timeout(static_on_timeout, Duration::from_secs(3));
14
15    // Waiting before cancelling this instance of spawn_timeout.
16    thread::sleep(Duration::from_millis(1500));
17
18    cancel();
19
20    println!("The first instance of spawn_timeout has been succesfully stopped");
21
22    // Sleeping for a long time for the sake of this example.
23    thread::sleep(Duration::from_millis(u64::MAX));
24}
examples/capturing_inner_function.rs (line 29)
7fn main() {
8    // The data shared with the given callback has to be thread-safe.
9    // https://doc.rust-lang.org/nomicon/send-and-sync.html
10    let counter = Arc::new(Mutex::new(2));
11    let cancel_first_option: Arc<Mutex<Option<Box<dyn Fn() + Send + 'static>>>> =
12        Arc::new(Mutex::new(None));
13    let cancel_first_option_clone = cancel_first_option.clone();
14
15    let on_timeout = move || {
16        let mut c = counter.lock().unwrap();
17        *c -= 1;
18        println!("{}", c);
19        if *c == 0 {
20            cancel_first_option_clone.lock().unwrap().as_ref().unwrap()();
21            println!("The first instance of spawn_timeout has been succesfully stopped");
22        }
23    };
24
25    // Leaking this inner function to make its lifetime as 'static.
26    // https://doc.rust-lang.org/std/boxed/struct.Box.html#method.leak
27    let static_on_timeout = Box::leak(Box::new(on_timeout));
28
29    let cancel_first = spawn_timeout::spawn_timeout(static_on_timeout, Duration::from_secs(3));
30    let _ = spawn_timeout::spawn_timeout(static_on_timeout, Duration::from_secs(2));
31    let _ = spawn_timeout::spawn_timeout(static_on_timeout, Duration::from_secs(1));
32
33    *cancel_first_option.lock().unwrap() = Some(cancel_first);
34
35    // Sleeping for a long time for the sake of this example.
36    thread::sleep(Duration::from_millis(u64::MAX));
37}