monitor

Crates.iomonitor
lib.rsmonitor
version0.1.0
sourcesrc
created_at2015-09-12 13:53:35.928544
updated_at2016-07-03 12:45:58.485817
descriptionMonitor synchronization construct
homepagehttps://github.com/kirillkh/monitor_rs
repositoryhttps://github.com/kirillkh/monitor_rs
max_upload_size
id3036
size15,626
Core (github:first-rust-competition:core)

documentation

README

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.

Commit count: 16

cargo fmt