#interval #multi-threading #timer #async #constant-time #time

已撤销 set_interval

在恒定的时间间隔调用子程序

0.1.1 2022年11月10日
0.1.0 2022年11月10日

#13 in #多线程

MIT 许可证

6KB

set_interval

在恒定的时间间隔调用子程序。

示例

基础

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

fn on_tick() {
    println!("tick!");
}

fn main() {
    let cancel = set_interval::set_interval(&on_tick, Duration::from_millis(500));

    // Waiting before cancelling this instance of set_interval.
    thread::sleep(Duration::from_secs(3));

    cancel();

    println!("This instance of set_interval has been succesfully stopped");

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

内部函数

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

fn main() {
    let on_tick = || {
        println!("tick!");
    };

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

    let cancel = set_interval::set_interval(static_on_tick, Duration::from_millis(500));

    // Waiting before cancelling this instance of set_interval.
    thread::sleep(Duration::from_secs(3));

    cancel();

    println!("This instance of set_interval has been succesfully stopped");

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

捕获内部函数

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

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

    let on_tick = move || {
        let mut c = counter.lock().unwrap();
        if *c == 0 {
            cancel_option_clone.lock().unwrap().as_ref().unwrap()();
            println!("We have a liftoff!");
            println!("This instance of set_interval has been succesfully stopped");
        } else {
            println!("{}", c);
            *c -= 1;
        }
    };

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

    let cancel = set_interval::set_interval(static_on_tick, Duration::from_secs(1));

    *cancel_option.lock().unwrap() = Some(cancel);

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

无运行时依赖