#clock #thread #time #action #delay #time-tracking #synchronization

yanked thread_clock

A crate that can be used to synchronize and delay actions between threads

3 unstable releases

0.2.1 Aug 3, 2023
0.2.0 Oct 26, 2022
0.1.0 Oct 25, 2022

#14 in #actions

MIT license

17KB
183 lines

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

Dependencies

~2.4–8MB
~66K SLoC