Created
August 22, 2019 07:33
-
-
Save rust-play/b9e0bda958d88f28b0c4c9e936ecbaed 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}, | |
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