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

#1324 in Concurrency

Download history 913/week @ 2024-11-23 1279/week @ 2024-11-30 1210/week @ 2024-12-07 975/week @ 2024-12-14 203/week @ 2024-12-21 227/week @ 2024-12-28 899/week @ 2025-01-04 1302/week @ 2025-01-11 1232/week @ 2025-01-18 1280/week @ 2025-01-25 922/week @ 2025-02-01 895/week @ 2025-02-08 709/week @ 2025-02-15 755/week @ 2025-02-22 811/week @ 2025-03-01 750/week @ 2025-03-08

3,109 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