Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created August 22, 2019 07:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/b9e0bda958d88f28b0c4c9e936ecbaed to your computer and use it in GitHub Desktop.
Save rust-play/b9e0bda958d88f28b0c4c9e936ecbaed to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
use std::{
sync::{Arc, Mutex},
thread,
};
struct Test {
flag: bool,
}
impl Test {
fn new() -> Self {
let test = Test { flag: false };
test
}
fn check_flag(&self) -> bool {
println!("flag {}", &self.flag);
return self.flag;
}
fn set_flag(&mut self) {
println!("set flag");
self.flag = true
}
}
fn main() {
let test = Arc::new(Mutex::new(Box::new(Test::new())));
let test2 = test.clone();
thread::spawn(move || {
thread::sleep_ms(1500);
println!("set");
test2.lock().expect("thread 1").set_flag();
});
loop {
let flag = test.lock().expect("main 1").check_flag();
thread::sleep_ms(500);
if flag {
break;
}
}
println!("end");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment