Yoric / timer.rs

Simple implementation of a timer for Rust
Mozilla Public License 2.0
40 stars 21 forks source link

Guard::ignore doesn't work #13

Open spfuetzner opened 4 years ago

spfuetzner commented 4 years ago

The following code just shows "test" for the 5 seconds while the first sleep() is running. So the guards isn't actually ignored.

fn main()
{
    {
        let timer = timer::Timer::new();
        let now = chrono::offset::Local::now();
        let repeat = chrono::Duration::seconds(1);
        let guard = timer.schedule(now, Some(repeat), || println!("test"));
        guard.ignore();
        std::thread::sleep(std::time::Duration::from_secs(5));
    }
    std::thread::sleep(std::time::Duration::from_secs(1000));
}
Yoric commented 4 years ago

Oh... not good.

Yoric commented 4 years ago

I don't know if I'll have time to work on this, but I'll definitely review patches!

0xpr03 commented 4 years ago

I can take a look at this, if you want @spfuetzner

wescande commented 4 years ago

Hi everyone! This is not a bug from timer.rs, but from the test code. When exit the scope, the timer is drop, therefore it will cancel it. The guard.ignore() works by doing so:

fn main()
{
    let timer = timer::Timer::new();
    {
        let now = chrono::offset::Local::now();
        let repeat = chrono::Duration::seconds(1);
        let guard = timer.schedule(now, Some(repeat), || println!("test"));
        guard.ignore();
        std::thread::sleep(std::time::Duration::from_secs(5));
    }
    std::thread::sleep(std::time::Duration::from_secs(1000));
}
0xpr03 commented 4 years ago

Ah thought so. Maybe we should document that you can't drop the timer itself. (And will have to start it inside a new thread to daemonize)