Created
November 13, 2019 19:01
-
-
Save rust-play/3bbbf390ce54c1308180aef8fac4ea3b to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::sync::{Arc, Mutex}; | |
pub struct Foo { | |
pub a: i32, | |
pub b: i32, | |
} | |
pub fn function(foo: Arc<Mutex<Foo>>){ | |
let foo = &mut *(foo.lock().unwrap()); | |
inner(&mut foo.a, &mut foo.b); | |
} | |
fn inner(a: &mut i32, b:&mut i32){ | |
*a += 1; | |
*b += *a; | |
} | |
fn main(){ | |
let x = Arc::new(Mutex::new(Foo{a :10, b: 20})); | |
function(x.clone()); | |
function(x.clone()); | |
let x = x.lock().unwrap(); | |
println!("{} {}", x.a, x.b); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment