Skip to content

Instantly share code, notes, and snippets.

@pepoviola
Last active October 22, 2020 18:21
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 pepoviola/0013ca3e946eea3b6e6fde0ea0c9d219 to your computer and use it in GitHub Desktop.
Save pepoviola/0013ca3e946eea3b6e6fde0ea0c9d219 to your computer and use it in GitHub Desktop.
Rust sleep in main...
/// 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