3 ms·
It will get even simpler when 1.63 hits with the `static` `Mutex::new`: use std::{sync::Mutex, thread, thread::sleep, time::Duration}; fn main() {
by drogus 4y ago
It will get even simpler when 1.63 hits with the `static` `Mutex::new`:
use std::{sync::Mutex, thread, thread::sleep, time::Duration};
fn main() {
static MUTEX: Mutex<String> = Mutex::new(String::new());
let t1 = thread::spawn(|| {
MUTEX.lock().unwrap().push_str("hello ");
});
let t2 = thread::spawn(|| {
sleep(Duration::from_millis(10));
MUTEX.lock().unwrap().push_str("world!");
});
t1.join().unwrap();
t2.join().unwrap();
println!("{}", MUTEX.lock().unwrap());
}
Of course you can't really use it for everything (ie. whatever you put inside `Mutex::new()` needs to be static as well, but still, it's a nice option too