thread_clock

Crates.iothread_clock
lib.rsthread_clock
version0.2.1
sourcesrc
created_at2022-10-25 23:41:42.28997
updated_at2023-08-03 07:42:59.241942
descriptionA crate that can be used to synchronize and delay actions between threads
homepage
repositoryhttps://github.com/LinkTheDot/Thread-Clock
max_upload_size
id697396
size27,884
(LinkTheDot)

documentation

README

NOTE

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

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.

Examples

Using clock for time

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);
}

Using clock for thread synching

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);
}
Commit count: 35

cargo fmt