4 releases

Uses old Rust 2015

0.1.0 Jul 3, 2016
0.0.3 Sep 12, 2015
0.0.2 Sep 12, 2015
0.0.1 Sep 12, 2015

#16 in #construct

Download history 225/week @ 2024-03-13 174/week @ 2024-03-20 150/week @ 2024-03-27 172/week @ 2024-04-03 247/week @ 2024-04-10 216/week @ 2024-04-17 259/week @ 2024-04-24 234/week @ 2024-05-01 212/week @ 2024-05-08 224/week @ 2024-05-15 214/week @ 2024-05-22 225/week @ 2024-05-29 178/week @ 2024-06-05 195/week @ 2024-06-12 237/week @ 2024-06-19 172/week @ 2024-06-26

821 downloads per month
Used in 5 crates (3 directly)

Custom license

15KB
339 lines

monitor_rs

A convenience library that provides an easier way to use the combination of Mutex+Condvar in Rust. The concept is known as Monitor synchronization construct and is similar to Java's synchronized() statement.

License: MIT

Usage

Put this in your Cargo.toml:

[dependencies]
monitor = "0.1.0"

And this in your crate root:

extern crate monitor;

Example

extern crate monitor;

use std::time::Duration;
use std::sync::Arc;
use std::thread;
use monitor::Monitor;

fn main() {
    let mon = Arc::new(Monitor::new(false));
    {
        let mon = mon.clone();
        let _ = thread::spawn(move || {
            thread::sleep(Duration::from_millis(1000));
            
            mon.with_lock(|mut done| {     // done is a monitor::MonitorGuard<bool>
                *done = true;
                done.notify_one();
            });
        });
    }
    
    mon.with_lock(|mut done| {
        while !*done {
            done.wait();
        }
        println!("finished waiting");
    });
}

For more examples, see the tests in lib.rs.

No runtime deps