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

#18 in #construct

Download history 674/week @ 2024-09-06 582/week @ 2024-09-13 970/week @ 2024-09-20 846/week @ 2024-09-27 836/week @ 2024-10-04 860/week @ 2024-10-11 1017/week @ 2024-10-18 740/week @ 2024-10-25 1030/week @ 2024-11-01 987/week @ 2024-11-08 979/week @ 2024-11-15 1109/week @ 2024-11-22 1125/week @ 2024-11-29 1253/week @ 2024-12-06 996/week @ 2024-12-13 292/week @ 2024-12-20

3,797 downloads per month
Used in 3 crates (2 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