Last active
October 22, 2020 18:21
-
-
Save pepoviola/0013ca3e946eea3b6e6fde0ea0c9d219 to your computer and use it in GitHub Desktop.
Rust sleep in main...
This file contains hidden or 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
/// playground link https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=4dc3d523c44e52a90368a3ada41340d5 | |
use std::time::Duration; | |
use std::thread; | |
use std::thread::sleep; | |
use std::sync::{Arc,RwLock}; | |
fn main() { | |
let story_behind_arc = Arc::new( RwLock::new( String::from("Once upon a time..." ))); | |
let story_ref = story_behind_arc.clone(); | |
let other_thread = thread::spawn( move || { | |
// let story_ref_in_thread = story_arc.clone(); | |
let mut story = story_ref.write().unwrap(); | |
*story += "Everyone lived happly ever after."; | |
sleep(Duration::new(0,100)); | |
println!("{}", story); | |
}); | |
{ | |
let story = story_behind_arc.read().unwrap(); | |
println!("{}", story); | |
} | |
let _result = other_thread.join(); | |
thread::sleep(Duration::new(0,200)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment