Crates.io | timer_list |
lib.rs | timer_list |
version | 0.1.0 |
source | src |
created_at | 2024-07-17 07:52:40.365937 |
updated_at | 2024-07-17 07:52:40.365937 |
description | A list of timed events that will be triggered sequentially when the timer expires |
homepage | https://github.com/arceos-org/arceos |
repository | https://github.com/arceos-org/timer_list |
max_upload_size | |
id | 1305953 |
size | 10,746 |
A list of timed events that will be triggered sequentially when the timer expires.
use timer_list::{TimerEvent, TimerEventFn, TimerList};
use std::time::{Duration, Instant};
let mut timer_list = TimerList::new();
// set a timer that will be triggered after 1 second
let start_time = Instant::now();
timer_list.set(Duration::from_secs(1), TimerEventFn::new(|now| {
println!("timer event after {:?}", now);
}));
while !timer_list.is_empty() {
// check if there is any event that is expired
let now = Instant::now().duration_since(start_time);
if let Some((deadline, event)) = timer_list.expire_one(now) {
// trigger the event, will print "timer event after 1.00s"
event.callback(now);
break;
}
std::thread::sleep(Duration::from_millis(10)); // relax the CPU
}