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

#13 in #construct

Download history 170/week @ 2024-01-08 128/week @ 2024-01-15 166/week @ 2024-01-22 139/week @ 2024-01-29 264/week @ 2024-02-05 171/week @ 2024-02-12 212/week @ 2024-02-19 203/week @ 2024-02-26 281/week @ 2024-03-04 305/week @ 2024-03-11 193/week @ 2024-03-18 141/week @ 2024-03-25 182/week @ 2024-04-01 207/week @ 2024-04-08 241/week @ 2024-04-15 223/week @ 2024-04-22

867 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