Crates.io | thread_clock |
lib.rs | thread_clock |
version | 0.2.1 |
source | src |
created_at | 2022-10-25 23:41:42.28997 |
updated_at | 2023-08-03 07:42:59.241942 |
description | A crate that can be used to synchronize and delay actions between threads |
homepage | |
repository | https://github.com/LinkTheDot/Thread-Clock |
max_upload_size | |
id | 697396 |
size | 27,884 |
This crate is very unreliable with tracking time. On top of that, your program will panic if you try to use Thread Clock with tokio.
Thread Clock was program made by me to gain more experience is writing code in general. Therefore, it won't be very efficient or well written.
Thread Clock will allow you to create a clock which can give the amount of ticks that have passed since it has started.
The purpose of this clock is to allow synchronization of actions between threads as the clock can be cloned to run anywhere. However using this for actual time tracking may be a bit iffy as every tick has approximately a +1.4ms drift.
use thread_clock::Clock;
fn main() {
let mut clock = Clock::new().unwrap();
clock.start();
clock.wait_for_time(50);
let time = clock.stop().unwrap();
assert_eq!(time, 51);
}
use thread_clock::Clock;
use std::thread;
fn main() {
let mut clock = Clock::new().unwrap();
clock.start();
let mut time_receiver = clock.spawn_receiver();
let handle = thread::spawn(move || {
for _ in 0..5 {
time_receiver.wait_for_tick();
}
let time = time_receiver.time();
assert_eq!(time, 5);
});
for _ in 0..5 {
clock.wait_for_tick();
}
let time = clock.time();
assert_eq!(time, 5);
let _ = handle.join();
let final_time = clock.stop().unwrap();
assert_eq!(final_time, 6);
}