| Crates.io | ztimer |
| lib.rs | ztimer |
| version | 0.1.2 |
| created_at | 2024-09-17 22:20:51.054842+00 |
| updated_at | 2024-09-20 00:13:36.155185+00 |
| description | A block-based, non-circular double-linked list implementation for Rust. |
| homepage | |
| repository | https://github.com/pecktalk/pex/tree/main/ztimer |
| max_upload_size | |
| id | 1378397 |
| size | 195,836 |
ztimer (Zero-Overhead Timer) provides a highly efficient, near O(1) timer system for Rust applications. It assumes that, in most use cases, the number of timer expirations is limited, eliminating the need to support a huge number of expirations, and reducing overhead.
AutoDropTimer instance is dropped before expiration.Clock can manage multiple timers within the same process, handling multiple timer groups based on your application needs.Timer instances can be canceled before they expire, avoiding callback invocations.Timer: A direct reference to a real timer instance stored in a Clock. It holds a callback that gets invoked upon expiration and can be canceled.AutoDropTimer: A wrapper around Timer that cancels the timer automatically when dropped, preventing further callback invocations.Clock: Groups and manages timers. A Clock can hold several timers, and you can have multiple Clock instances in a process.AutoDropTimerHere’s an example of how to use AutoDropTimer to manage timers in your application:
use std::thread::sleep;
use std::time::Duration;
use ztimer::{AutoDropTimer, Clock};
// Create a clock instance
let clock = Clock::new(None).unwrap();
// Create some timers
let t1 = AutoDropTimer::new(clock, Duration::from_secs(1), || {
println!("T1 expired");
}, "t1".to_string()).unwrap();
let t2 = AutoDropTimer::new(clock, Duration::from_secs(10), || {
println!("T2 expired");
}, "t2".to_string()).unwrap();
// Use sleep to simulate the passage of time
sleep(Duration::from_secs(2)); // T1 expires
assert_eq!(clock.len(), 2);
If your application needs to wait for the underlying timer thread before exiting, here's what you should do:
use ztimer::Clock;
// Create a clock instance
let _ = Clock::new(None);
// Before terminating:
let tjh = Clock::take_thread_handle();
Clock::terminate_for_process_exit();
let _ = tjh.unwrap().join();
This crate is licensed under the Apache License, Version 2.0. See LICENSE for details.