-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01-spawned-thread.rs
More file actions
29 lines (23 loc) · 1.07 KB
/
01-spawned-thread.rs
File metadata and controls
29 lines (23 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// cargo run --bin 01-spawned-thread
use std::thread;
use std::time::Duration;
fn main() {
let handle = thread::spawn(|| {
for i in 1..10 {
println!("hi number {i} from the spawned thread!");
thread::sleep(Duration::from_millis(1)); // The calls `thread::sleep` force a thread to stop its execution for a short duration, allowing a different thread to run.
}
});
// 2) If we move `handle.join()` before main thread, the main thread will wait for the spawned thread to finish and then run their for loop.
//
// try uncomment this:
// handle.join().unwrap();
for i in 1..5 {
println!("hi number {i} from the main thread");
thread::sleep(Duration::from_millis(1));
}
// 1) In the first, the two threads continue alternating,until end of *main thread for loop*, the `handle.join()` tell main thread can't end until spawned thread is finished.
//
// Note: If we don't call handle.join(), the spawned thread will be shutdown immediately, when main thread finish.
handle.join().unwrap();
}