Crates.io | monitor |
lib.rs | monitor |
version | 0.1.0 |
source | src |
created_at | 2015-09-12 13:53:35.928544 |
updated_at | 2016-07-03 12:45:58.485817 |
description | Monitor synchronization construct |
homepage | https://github.com/kirillkh/monitor_rs |
repository | https://github.com/kirillkh/monitor_rs |
max_upload_size | |
id | 3036 |
size | 15,626 |
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
Put this in your Cargo.toml
:
[dependencies]
monitor = "0.1.0"
And this in your crate root:
extern crate monitor;
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.