Struct std::old_io::timer::TimerUnstable
[-] [+]
[src]
pub struct Timer { // some fields omitted }
A synchronous timer object
Values of this type can be used to put the current task to sleep for a period of time. Handles to this timer can also be created in the form of receivers which will receive notifications over time.
Examples
fn main() { fn foo() { use std::old_io::Timer; use std::time::Duration; let mut timer = Timer::new().unwrap(); timer.sleep(Duration::milliseconds(10)); // block the task for awhile let timeout = timer.oneshot(Duration::milliseconds(10)); // do some work timeout.recv().unwrap(); // wait for the timeout to expire let periodic = timer.periodic(Duration::milliseconds(10)); loop { periodic.recv().unwrap(); // this loop is only executed once every 10ms } } }use std::old_io::Timer; use std::time::Duration; let mut timer = Timer::new().unwrap(); timer.sleep(Duration::milliseconds(10)); // block the task for awhile let timeout = timer.oneshot(Duration::milliseconds(10)); // do some work timeout.recv().unwrap(); // wait for the timeout to expire let periodic = timer.periodic(Duration::milliseconds(10)); loop { periodic.recv().unwrap(); // this loop is only executed once every 10ms }
If only sleeping is necessary, then a convenience API is provided through
the old_io::timer
module.
use std::old_io::timer; use std::time::Duration; // Put this task to sleep for 5 seconds timer::sleep(Duration::seconds(5));
Methods
impl Timer
fn new() -> IoResult<Timer>
Creates a new timer which can be used to put the current task to sleep for a number of milliseconds, or to possibly create channels which will get notified after an amount of time has passed.
fn sleep(&mut self, duration: Duration)
Blocks the current task for the specified duration.
Note that this function will cause any other receivers for this timer to be invalidated (the other end will be closed).
When provided a zero or negative duration
, the function will
return immediately.
fn oneshot(&mut self, duration: Duration) -> Receiver<()>
Creates a oneshot receiver which will have a notification sent when the specified duration has elapsed.
This does not block the current task, but instead returns immediately.
Note that this invalidates any previous receiver which has been created
by this timer, and that the returned receiver will be invalidated once
the timer is destroyed (when it falls out of scope). In particular, if
this is called in method-chaining style, the receiver will be
invalidated at the end of that statement, and all recv
calls will
fail.
Example
fn main() { use std::old_io::Timer; use std::time::Duration; let mut timer = Timer::new().unwrap(); let ten_milliseconds = timer.oneshot(Duration::milliseconds(10)); for _ in 0..100 { /* do work */ } // blocks until 10 ms after the `oneshot` call ten_milliseconds.recv().unwrap(); }use std::old_io::Timer; use std::time::Duration; let mut timer = Timer::new().unwrap(); let ten_milliseconds = timer.oneshot(Duration::milliseconds(10)); for _ in 0..100 { /* do work */ } // blocks until 10 ms after the `oneshot` call ten_milliseconds.recv().unwrap();fn main() { use std::old_io::Timer; use std::time::Duration; // Incorrect, method chaining-style: let mut five_ms = Timer::new().unwrap().oneshot(Duration::milliseconds(5)); // The timer object was destroyed, so this will always fail: // five_ms.recv().unwrap() }
use std::old_io::Timer; use std::time::Duration; // Incorrect, method chaining-style: let mut five_ms = Timer::new().unwrap().oneshot(Duration::milliseconds(5)); // The timer object was destroyed, so this will always fail: // five_ms.recv().unwrap()
When provided a zero or negative duration
, the message will
be sent immediately.
fn periodic(&mut self, duration: Duration) -> Receiver<()>
Creates a receiver which will have a continuous stream of notifications being sent each time the specified duration has elapsed.
This does not block the current task, but instead returns immediately. The first notification will not be received immediately, but rather after the first duration.
Note that this invalidates any previous receiver which has been created
by this timer, and that the returned receiver will be invalidated once
the timer is destroyed (when it falls out of scope). In particular, if
this is called in method-chaining style, the receiver will be
invalidated at the end of that statement, and all recv
calls will
fail.
Example
fn main() { use std::old_io::Timer; use std::time::Duration; let mut timer = Timer::new().unwrap(); let ten_milliseconds = timer.periodic(Duration::milliseconds(10)); for _ in 0..100 { /* do work */ } // blocks until 10 ms after the `periodic` call ten_milliseconds.recv().unwrap(); for _ in 0..100 { /* do work */ } // blocks until 20 ms after the `periodic` call (*not* 10ms after the // previous `recv`) ten_milliseconds.recv().unwrap(); }use std::old_io::Timer; use std::time::Duration; let mut timer = Timer::new().unwrap(); let ten_milliseconds = timer.periodic(Duration::milliseconds(10)); for _ in 0..100 { /* do work */ } // blocks until 10 ms after the `periodic` call ten_milliseconds.recv().unwrap(); for _ in 0..100 { /* do work */ } // blocks until 20 ms after the `periodic` call (*not* 10ms after the // previous `recv`) ten_milliseconds.recv().unwrap();fn main() { use std::old_io::Timer; use std::time::Duration; // Incorrect, method chaining-style. let mut five_ms = Timer::new().unwrap().periodic(Duration::milliseconds(5)); // The timer object was destroyed, so this will always fail: // five_ms.recv().unwrap() }
use std::old_io::Timer; use std::time::Duration; // Incorrect, method chaining-style. let mut five_ms = Timer::new().unwrap().periodic(Duration::milliseconds(5)); // The timer object was destroyed, so this will always fail: // five_ms.recv().unwrap()
When provided a zero or negative duration
, the messages will
be sent without delay.