Skip to content

kirillkh/monitor_rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Monitor: synchronization construct in Rust

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages