microlock 0.3.0

A crate for waiting: Small locks and other timing things!
Documentation
  • Coverage
  • 53.33%
    16 out of 30 items documented0 out of 21 items with examples
  • Size
  • Source code size: 18.91 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 2.82 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 24s Average build duration of successful builds.
  • all releases: 16s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • TudbuT

microlock

Microlock is a smallish crate for waiting. It contains an UntimedLock suitable for complex synchronization, and a TimedLock that can also function as such, but additionally provides timer functionalities.

Use-case:

The untimed lock can be used similarly to a channel, with added flexibility. In this example, it simply replaces a channel, but more complex use-cases are significantly less trivial with channels.

use std::{
    collections::VecDeque,
    sync::{Arc, Mutex},
    thread,
};

use microlock::{Lock, TimedLock, UntimedLock};

fn main() {
    static LOCK: UntimedLock = UntimedLock::locked();
    let queue = Arc::new(Mutex::new(VecDeque::new()));
    let queue2 = queue.clone();
    thread::spawn(move || loop {
        LOCK.wait_here();
        println!("{}", queue2.lock().unwrap().pop_front().unwrap());
        // try_lock because another unlock *could*ve happened since then
        LOCK.try_lock();
    });

    let timer = TimedLock::unlocked();
    for i in 0..5 {
        timer.lock_for_ms(100);
        println!("Sending {i}...");
        queue.lock().unwrap().push_back(format!("Hello! {i}"));
        LOCK.unlock();
        println!("Sent!");
        timer.wait_here();
    }
}