Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created July 17, 2018 20:10
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 rust-play/76ac8edabae38a2a30a3be43074cb55f to your computer and use it in GitHub Desktop.
Save rust-play/76ac8edabae38a2a30a3be43074cb55f to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
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