Skip to content

Commit 9c99742

Browse files
committed
Add example of Join Handles in Document B09 Thread md
1 parent bde94de commit 9c99742

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

documentation2/B09-Thread.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,78 @@ cargo run
6363
4 from the main thread!
6464
3 from the spawned thread!
6565
```
66+
67+
In the example above, we create a thread using the `thread::spawn()` function. The thread loops over `0..5` and prints the current value.
68+
69+
Similarly, we have a main thread where we loop over `0..5` and print the current value.
70+
71+
We also call `thread::sleep` to force a thread to stop its execution for a short duration, allowing a different thread to run.
72+
73+
Notice that we sleep **2** milliseconds in the spawned thread and 1 millisecond in the main thread.
74+
75+
The output from this program might be a little different every time. The important thing to remember here is that if the main thread completes, all other threads are shut down whether or not they have finished running.
76+
77+
So, even though the spawned thread should print until `i` is 9, it only reaches to **2** because the main thread shut down.
78+
79+
____
80+
81+
### Join Handles in Rust
82+
83+
A spawned thread always returns a join handle. If we want the spawned thread to complete execution, we can save the return value of `thread::spawn` in a variable and then call the `join()` method on it.
84+
85+
The `join()` method on `JoinHandle` (return type of `thread::spawn`) waits for the spawned thread to finish.
86+
87+
Let's look at an example.
88+
89+
```rust
90+
use std::thread;
91+
use std::time::Duration;
92+
93+
fn main() {
94+
// create a thread and save the handle to a variable
95+
let handle = thread::spawn(|| {
96+
// everything in here runs in a separate thread
97+
for i in 0..10 {
98+
println!("{} from the spawned thread!", i);
99+
thread::sleep(Duration::from_millis(2));
100+
}
101+
});
102+
103+
// main thread
104+
for i in 0..5 {
105+
println!("{} from the main thread!", i);
106+
thread::sleep(Duration::from_millis(1));
107+
}
108+
109+
// wait for the separate thread to complete
110+
handle.join().unwrap();
111+
}
112+
```
113+
114+
```bash
115+
cargo build
116+
```
117+
118+
```bash
119+
cargo run
120+
```
121+
122+
#### Output
123+
124+
```bash
125+
0 from the main thread!
126+
0 from the spawned thread!
127+
1 from the main thread!
128+
1 from the spawned thread!
129+
2 from the main thread!
130+
3 from the main thread!
131+
2 from the spawned thread!
132+
4 from the main thread!
133+
3 from the spawned thread!
134+
4 from the spawned thread!
135+
5 from the spawned thread!
136+
6 from the spawned thread!
137+
7 from the spawned thread!
138+
8 from the spawned thread!
139+
9 from the spawned thread!
140+
```

programiz/attempt-02/hello_world/src/main.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use std::thread;
22
use std::time::Duration;
33

44
fn main() {
5-
// create a thread
6-
thread::spawn(|| {
5+
// create a thread and save the handle to a variable
6+
let handle = thread::spawn(|| {
77
// everything in here runs in a separate thread
88
for i in 0..10 {
99
println!("{} from the spawned thread!", i);
@@ -16,4 +16,7 @@ fn main() {
1616
println!("{} from the main thread!", i);
1717
thread::sleep(Duration::from_millis(1));
1818
}
19+
20+
// wait for the separate thread to complete
21+
handle.join().unwrap();
1922
}

0 commit comments

Comments
 (0)