Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created November 13, 2019 19:01
Show Gist options
  • Save rust-play/3bbbf390ce54c1308180aef8fac4ea3b to your computer and use it in GitHub Desktop.
Save rust-play/3bbbf390ce54c1308180aef8fac4ea3b to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
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