Created
May 27, 2020 11:00
-
-
Save Coutlaw/cc83197f6f37bb7113aa7c15e1e443ef to your computer and use it in GitHub Desktop.
Thread Spawn in rust
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
use std::thread; | |
use std::time::Duration; | |
fn main() { | |
// this thread will die when the main thread has ended | |
// but this spawned thread will start printing outputs while the main thread continues on | |
thread::spawn(|| { | |
for i in 1..10 { | |
println!("hi number {} from the spawned thread!", i); | |
thread::sleep(Duration::from_millis(1)); | |
} | |
}); | |
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