Giter Site home page Giter Site logo

rust_spawn_timeout's Introduction

spawn_timeout

Call a subroutine after a constant time interval.

Examples

Basic

use std::{thread, time::Duration};
use spawn_timeout;

fn on_timeout() {
    println!("timeout!");
}

fn main() {
    let cancel = spawn_timeout::spawn_timeout(&on_timeout, Duration::from_secs(3));
    let _ = spawn_timeout::spawn_timeout(&on_timeout, Duration::from_secs(3));

    // Waiting before cancelling this instance of spawn_timeout.
    thread::sleep(Duration::from_millis(1500));

    cancel();

    println!("The first instance of spawn_timeout has been succesfully stopped");

    // Sleeping for a long time for the sake of this example.
    thread::sleep(Duration::from_millis(u64::MAX));
}

Inner function

use std::{thread, time::Duration};
use spawn_timeout;

fn main() {
    let on_timeout = || {
        println!("timeout!");
    };

    // Leaking this inner function to make its lifetime as 'static.
    // https://doc.rust-lang.org/std/boxed/struct.Box.html#method.leak
    let static_on_timeout = Box::leak(Box::new(on_timeout));

    let cancel = spawn_timeout::spawn_timeout(static_on_timeout, Duration::from_secs(3));
    let _ = spawn_timeout::spawn_timeout(static_on_timeout, Duration::from_secs(3));

    // Waiting before cancelling this instance of spawn_timeout.
    thread::sleep(Duration::from_millis(1500));

    cancel();

    println!("The first instance of spawn_timeout has been succesfully stopped");

    // Sleeping for a long time for the sake of this example.
    thread::sleep(Duration::from_millis(u64::MAX));
}

Capturing inner function

use std::{
    sync::{Arc, Mutex},
    thread,
    time::Duration,
};
use spawn_timeout;

fn main() {
    // The data shared with the given callback has to be thread-safe.
    // https://doc.rust-lang.org/nomicon/send-and-sync.html
    let counter = Arc::new(Mutex::new(2));
    let cancel_first_option: Arc<Mutex<Option<Box<dyn Fn() + Send + 'static>>>> =
        Arc::new(Mutex::new(None));
    let cancel_first_option_clone = cancel_first_option.clone();

    let on_timeout = move || {
        let mut c = counter.lock().unwrap();
        *c -= 1;
        println!("{}", c);
        if *c == 0 {
            cancel_first_option_clone.lock().unwrap().as_ref().unwrap()();
            println!("The first instance of spawn_timeout has been succesfully stopped");
        }
    };

    // Leaking this inner function to make its lifetime as 'static.
    // https://doc.rust-lang.org/std/boxed/struct.Box.html#method.leak
    let static_on_timeout = Box::leak(Box::new(on_timeout));

    let cancel_first = spawn_timeout::spawn_timeout(static_on_timeout, Duration::from_secs(3));
    let _ = spawn_timeout::spawn_timeout(static_on_timeout, Duration::from_secs(2));
    let _ = spawn_timeout::spawn_timeout(static_on_timeout, Duration::from_secs(1));

    *cancel_first_option.lock().unwrap() = Some(cancel_first);

    // Sleeping for a long time for the sake of this example.
    thread::sleep(Duration::from_millis(u64::MAX));
}

rust_spawn_timeout's People

Contributors

bleaklake avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.