Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::sync::{Mutex};
- use std::thread;
- struct ThreadSafeStruct {
- w: Mutex<u64>,
- }
- impl ThreadSafeStruct {
- fn update(&self, val: u64) {
- let mut v = self.w.lock().unwrap();
- *v = val;
- }
- fn get(&self) -> u64 {
- let v = self.w.lock().unwrap();
- *v
- }
- }
- fn main() {
- let ilivelongenough: ThreadSafeStruct = ThreadSafeStruct {
- w: Mutex::new(200),
- };
- let ilivelongenough_ref = &ilivelongenough;
- let thread_handle = thread::spawn(move || {
- println!("Value of ilivelongenough in another thread is: {}.", ilivelongenough_ref.get());
- thread::sleep(std::time::Duration::from_secs(1));
- });
- thread_handle.join().unwrap_or_default();
- println!("ilivelongenough in this thread is: {}.", ilivelongenough.get());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement