Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::sync::{Arc, RwLock};
- use std::{thread, time};
- fn main() {
- let lock = Arc::new(RwLock::new(1));
- let c_lock = lock.clone();
- thread::spawn(move || {
- println!("Begin Read");
- let n = lock.read().unwrap();
- assert_eq!(*n, 1);
- println!("Sleeping");
- thread::sleep(time::Duration::from_millis(10000));
- });
- thread::spawn(move || {
- thread::sleep(time::Duration::from_millis(2000));
- println!("Begin Write");
- let r = c_lock.write();
- assert!(r.is_ok());
- thread::sleep(time::Duration::from_millis(5000));
- println!("Done Write");
- });
- loop {
- thread::sleep(time::Duration::from_millis(100));
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement