SHARE
TWEET

Untitled

a guest Oct 15th, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use std::sync::RwLock;
  2.  
  3. fn main() {
  4.     // read config into mutex
  5.     let foo = RwLock::new(String::from("asd"));
  6.    
  7.     let read_lock_a = foo.read().unwrap();
  8.     let read_lock_b = foo.read().unwrap();
  9.     println!("{} {}",read_lock_a,read_lock_b); // two read locks at once
  10.     drop(read_lock_a);
  11.     drop(read_lock_b);
  12.    
  13.     // change foo
  14.     let mut write_lock = foo.write().unwrap();
  15.     *write_lock = String::from("asd");
  16.     // now store that back to a file if wanted
  17.     drop(write_lock);
  18.    
  19.     // read again, from memory, we don't re-read anything
  20.     let read_lock = foo.read().unwrap();
  21.     println!("{}",read_lock);
  22. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top