Function chan::tick_ms[][src]

pub fn tick_ms(duration: u32) -> Receiver<Sender<()>>

Creates a new rendezvous channel that is "ticked" every duration.

duration is specified in milliseconds.

When duration is 0, no ticks are ever sent.

When duration is non-zero, then a new channel is created and sent at every duration. When the sent channel is dropped, the timer is reset and the process repeats after the duration.

This is especially convenient because it keeps the ticking in sync with the code that uses it. Namely, the ticks won't "build up."

N.B. There is no way to reclaim the resources used by this function. If you stop receiving on the channel returned, then the thread spawned by tick_ms will block indefinitely.

Examples

This is most useful when used in chan_select! because the received sentinel channel gets dropped only after the correspond arm has executed. At which point, the ticker is reset and waits to tick until duration milliseconds lapses after the chan_select! arm is executed.

use std::thread;
use std::time::Duration;

let tick = chan::tick_ms(100);
let boom = chan::after_ms(500);
loop {
    chan_select! {
        default => {
            println!("   .");
            thread::sleep(Duration::from_millis(50));
        },
        tick.recv() => println!("tick."),
        boom.recv() => { println!("BOOM!"); return; },
    }
}