Created
July 17, 2018 20:10
-
-
Save rust-play/76ac8edabae38a2a30a3be43074cb55f 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::thread; | |
use std::time::Duration; | |
// Yay multithreading! | |
// all examples stolen shamelessly from the Rust book | |
// my first thread example | |
fn main() { | |
// spawn a SINGLE THREAD! and return the TID | |
let handle = thread::spawn(|| { | |
for i in 1..10 { | |
println!("hi number {} from the spawned thread!", i); | |
thread::sleep(Duration::from_millis(1)); | |
} | |
}); | |
// handle.join().unwrap(); // call join() to block on spawned thread | |
for i in 1..5 { | |
println!("hi number {} from the main thread!", i); | |
thread::sleep(Duration::from_millis(1)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment