1 unstable release
0.1.0 | Sep 12, 2020 |
---|
#20 in #system-clock
8KB
123 lines
timeouts
Set timeouts on os_clocks. Currently depends on having a multi-threaded tokio runtime.
lib.rs
:
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();
}
Dependencies
~5.5–8MB
~141K SLoC