Expand description
Set timeouts on os_clocks.
Depends on having a multi-threaded tokio runtime.
§Example:
use std::time::Duration;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use os_clock::ThreadCPUClock;
use tokio;
#[tokio::main]
async fn main() {
// must be called inside a Tokio runtime.
let timer = timeouts::Timer::<ThreadCPUClock>::spawn();
let thread_handle = std::thread::spawn(move || {
let spinlock = Arc::new(AtomicUsize::new(1));
let spinlock_clone = spinlock.clone();
let timeout = &timer.set_timeout_on_current_thread_cpu_usage(Duration::from_millis(2), move || {
spinlock_clone.store(0, Ordering::Relaxed);
});
let mut i = 0;
while spinlock.load(Ordering::Relaxed) != 0 {
i += 1;
}
println!("Num iters: {:?}", i);
});
thread_handle.join();
}
Structs§
- Timeout
Handle - Timeout
Listener - Called on timeout
- Timer