]> git.proxmox.com Git - rustc.git/blame - src/doc/book/src/ch16-01-threads.md
New upstream version 1.37.0+dfsg1
[rustc.git] / src / doc / book / src / ch16-01-threads.md
CommitLineData
13cf67c4
XL
1## Using Threads to Run Code Simultaneously
2
3In most current operating systems, an executed program’s code is run in a
4*process*, and the operating system manages multiple processes at once. Within
5your program, you can also have independent parts that run simultaneously. The
6features that run these independent parts are called *threads*.
7
8Splitting the computation in your program into multiple threads can improve
9performance because the program does multiple tasks at the same time, but it
10also adds complexity. Because threads can run simultaneously, there’s no
11inherent guarantee about the order in which parts of your code on different
12threads will run. This can lead to problems, such as:
13
14* Race conditions, where threads are accessing data or resources in an
15 inconsistent order
16* Deadlocks, where two threads are waiting for each other to finish using a
17 resource the other thread has, preventing both threads from continuing
18* Bugs that happen only in certain situations and are hard to reproduce and fix
19 reliably
20
21Rust attempts to mitigate the negative effects of using threads, but
22programming in a multithreaded context still takes careful thought and requires
23a code structure that is different from that in programs running in a single
24thread.
25
26Programming languages implement threads in a few different ways. Many operating
27systems provide an API for creating new threads. This model where a language
28calls the operating system APIs to create threads is sometimes called *1:1*,
29meaning one operating system thread per one language thread.
30
31Many programming languages provide their own special implementation of threads.
32Programming language-provided threads are known as *green* threads, and
33languages that use these green threads will execute them in the context of a
34different number of operating system threads. For this reason, the
35green-threaded model is called the *M:N* model: there are `M` green threads per
36`N` operating system threads, where `M` and `N` are not necessarily the same
37number.
38
39Each model has its own advantages and trade-offs, and the trade-off most
40important to Rust is runtime support. *Runtime* is a confusing term and can
41have different meanings in different contexts.
42
43In this context, by *runtime* we mean code that is included by the language in
44every binary. This code can be large or small depending on the language, but
45every non-assembly language will have some amount of runtime code. For that
46reason, colloquially when people say a language has “no runtime,” they often
47mean “small runtime.” Smaller runtimes have fewer features but have the
48advantage of resulting in smaller binaries, which make it easier to combine the
49language with other languages in more contexts. Although many languages are
50okay with increasing the runtime size in exchange for more features, Rust needs
51to have nearly no runtime and cannot compromise on being able to call into C to
52maintain performance.
53
54The green-threading M:N model requires a larger language runtime to manage
55threads. As such, the Rust standard library only provides an implementation of
561:1 threading. Because Rust is such a low-level language, there are crates that
57implement M:N threading if you would rather trade overhead for aspects such as
58more control over which threads run when and lower costs of context switching,
59for example.
60
61Now that we’ve defined threads in Rust, let’s explore how to use the
62thread-related API provided by the standard library.
63
64### Creating a New Thread with `spawn`
65
66To create a new thread, we call the `thread::spawn` function and pass it a
67closure (we talked about closures in Chapter 13) containing the code we want to
68run in the new thread. The example in Listing 16-1 prints some text from a main
69thread and other text from a new thread:
70
71<span class="filename">Filename: src/main.rs</span>
72
73```rust
74use std::thread;
75use std::time::Duration;
76
77fn main() {
78 thread::spawn(|| {
79 for i in 1..10 {
80 println!("hi number {} from the spawned thread!", i);
81 thread::sleep(Duration::from_millis(1));
82 }
83 });
84
85 for i in 1..5 {
86 println!("hi number {} from the main thread!", i);
87 thread::sleep(Duration::from_millis(1));
88 }
89}
90```
91
92<span class="caption">Listing 16-1: Creating a new thread to print one thing
93while the main thread prints something else</span>
94
95Note that with this function, the new thread will be stopped when the main
96thread ends, whether or not it has finished running. The output from this
97program might be a little different every time, but it will look similar to the
98following:
99
100```text
101hi number 1 from the main thread!
102hi number 1 from the spawned thread!
103hi number 2 from the main thread!
104hi number 2 from the spawned thread!
105hi number 3 from the main thread!
106hi number 3 from the spawned thread!
107hi number 4 from the main thread!
108hi number 4 from the spawned thread!
109hi number 5 from the spawned thread!
110```
111
112The calls to `thread::sleep` force a thread to stop its execution for a short
113duration, allowing a different thread to run. The threads will probably take
114turns, but that isn’t guaranteed: it depends on how your operating system
115schedules the threads. In this run, the main thread printed first, even though
116the print statement from the spawned thread appears first in the code. And even
117though we told the spawned thread to print until `i` is 9, it only got to 5
118before the main thread shut down.
119
120If you run this code and only see output from the main thread, or don’t see any
121overlap, try increasing the numbers in the ranges to create more opportunities
122for the operating system to switch between the threads.
123
124### Waiting for All Threads to Finish Using `join` Handles
125
126The code in Listing 16-1 not only stops the spawned thread prematurely most of
127the time due to the main thread ending, but also can’t guarantee that the
128spawned thread will get to run at all. The reason is that there is no guarantee
129on the order in which threads run!
130
131We can fix the problem of the spawned thread not getting to run, or not getting
132to run completely, by saving the return value of `thread::spawn` in a variable.
133The return type of `thread::spawn` is `JoinHandle`. A `JoinHandle` is an owned
134value that, when we call the `join` method on it, will wait for its thread to
135finish. Listing 16-2 shows how to use the `JoinHandle` of the thread we created
136in Listing 16-1 and call `join` to make sure the spawned thread finishes before
137`main` exits:
138
139<span class="filename">Filename: src/main.rs</span>
140
141```rust
142use std::thread;
143use std::time::Duration;
144
145fn main() {
146 let handle = thread::spawn(|| {
147 for i in 1..10 {
148 println!("hi number {} from the spawned thread!", i);
149 thread::sleep(Duration::from_millis(1));
150 }
151 });
152
153 for i in 1..5 {
154 println!("hi number {} from the main thread!", i);
155 thread::sleep(Duration::from_millis(1));
156 }
157
158 handle.join().unwrap();
159}
160```
161
162<span class="caption">Listing 16-2: Saving a `JoinHandle` from `thread::spawn`
163to guarantee the thread is run to completion</span>
164
165Calling `join` on the handle blocks the thread currently running until the
166thread represented by the handle terminates. *Blocking* a thread means that
167thread is prevented from performing work or exiting. Because we’ve put the call
168to `join` after the main thread’s `for` loop, running Listing 16-2 should
169produce output similar to this:
170
171```text
172hi number 1 from the main thread!
173hi number 2 from the main thread!
174hi number 1 from the spawned thread!
175hi number 3 from the main thread!
176hi number 2 from the spawned thread!
177hi number 4 from the main thread!
178hi number 3 from the spawned thread!
179hi number 4 from the spawned thread!
180hi number 5 from the spawned thread!
181hi number 6 from the spawned thread!
182hi number 7 from the spawned thread!
183hi number 8 from the spawned thread!
184hi number 9 from the spawned thread!
185```
186
187The two threads continue alternating, but the main thread waits because of the
188call to `handle.join()` and does not end until the spawned thread is finished.
189
190But let’s see what happens when we instead move `handle.join()` before the
191`for` loop in `main`, like this:
192
193<span class="filename">Filename: src/main.rs</span>
194
195```rust
196use std::thread;
197use std::time::Duration;
198
199fn main() {
200 let handle = thread::spawn(|| {
201 for i in 1..10 {
202 println!("hi number {} from the spawned thread!", i);
203 thread::sleep(Duration::from_millis(1));
204 }
205 });
206
207 handle.join().unwrap();
208
209 for i in 1..5 {
210 println!("hi number {} from the main thread!", i);
211 thread::sleep(Duration::from_millis(1));
212 }
213}
214```
215
216The main thread will wait for the spawned thread to finish and then run its
217`for` loop, so the output won’t be interleaved anymore, as shown here:
218
219```text
220hi number 1 from the spawned thread!
221hi number 2 from the spawned thread!
222hi number 3 from the spawned thread!
223hi number 4 from the spawned thread!
224hi number 5 from the spawned thread!
225hi number 6 from the spawned thread!
226hi number 7 from the spawned thread!
227hi number 8 from the spawned thread!
228hi number 9 from the spawned thread!
229hi number 1 from the main thread!
230hi number 2 from the main thread!
231hi number 3 from the main thread!
232hi number 4 from the main thread!
233```
234
235Small details, such as where `join` is called, can affect whether or not your
236threads run at the same time.
237
238### Using `move` Closures with Threads
239
240The `move` closure is often used alongside `thread::spawn` because it allows
241you to use data from one thread in another thread.
242
243In Chapter 13, we mentioned we can use the `move` keyword before the parameter
244list of a closure to force the closure to take ownership of the values it uses
245in the environment. This technique is especially useful when creating new
246threads in order to transfer ownership of values from one thread to another.
247
248Notice in Listing 16-1 that the closure we pass to `thread::spawn` takes no
249arguments: we’re not using any data from the main thread in the spawned
250thread’s code. To use data from the main thread in the spawned thread, the
251spawned thread’s closure must capture the values it needs. Listing 16-3 shows
252an attempt to create a vector in the main thread and use it in the spawned
253thread. However, this won’t yet work, as you’ll see in a moment.
254
255<span class="filename">Filename: src/main.rs</span>
256
257```rust,ignore,does_not_compile
258use std::thread;
259
260fn main() {
261 let v = vec![1, 2, 3];
262
263 let handle = thread::spawn(|| {
264 println!("Here's a vector: {:?}", v);
265 });
266
267 handle.join().unwrap();
268}
269```
270
271<span class="caption">Listing 16-3: Attempting to use a vector created by the
272main thread in another thread</span>
273
274The closure uses `v`, so it will capture `v` and make it part of the closure’s
275environment. Because `thread::spawn` runs this closure in a new thread, we
276should be able to access `v` inside that new thread. But when we compile this
277example, we get the following error:
278
279```text
280error[E0373]: closure may outlive the current function, but it borrows `v`,
281which is owned by the current function
282 --> src/main.rs:6:32
283 |
2846 | let handle = thread::spawn(|| {
285 | ^^ may outlive borrowed value `v`
2867 | println!("Here's a vector: {:?}", v);
287 | - `v` is borrowed here
288 |
289help: to force the closure to take ownership of `v` (and any other referenced
290variables), use the `move` keyword
291 |
2926 | let handle = thread::spawn(move || {
293 | ^^^^^^^
294```
295
296Rust *infers* how to capture `v`, and because `println!` only needs a reference
297to `v`, the closure tries to borrow `v`. However, there’s a problem: Rust can’t
298tell how long the spawned thread will run, so it doesn’t know if the reference
299to `v` will always be valid.
300
301Listing 16-4 provides a scenario that’s more likely to have a reference to `v`
302that won’t be valid:
303
304<span class="filename">Filename: src/main.rs</span>
305
306```rust,ignore,does_not_compile
307use std::thread;
308
309fn main() {
310 let v = vec![1, 2, 3];
311
312 let handle = thread::spawn(|| {
313 println!("Here's a vector: {:?}", v);
314 });
315
316 drop(v); // oh no!
317
318 handle.join().unwrap();
319}
320```
321
322<span class="caption">Listing 16-4: A thread with a closure that attempts to
323capture a reference to `v` from a main thread that drops `v`</span>
324
325If we were allowed to run this code, there’s a possibility the spawned thread
326would be immediately put in the background without running at all. The spawned
327thread has a reference to `v` inside, but the main thread immediately drops
328`v`, using the `drop` function we discussed in Chapter 15. Then, when the
329spawned thread starts to execute, `v` is no longer valid, so a reference to it
330is also invalid. Oh no!
331
332To fix the compiler error in Listing 16-3, we can use the error message’s
333advice:
334
335```text
336help: to force the closure to take ownership of `v` (and any other referenced
337variables), use the `move` keyword
338 |
3396 | let handle = thread::spawn(move || {
340 | ^^^^^^^
341```
342
343By adding the `move` keyword before the closure, we force the closure to take
344ownership of the values it’s using rather than allowing Rust to infer that it
345should borrow the values. The modification to Listing 16-3 shown in Listing
34616-5 will compile and run as we intend:
347
348<span class="filename">Filename: src/main.rs</span>
349
350```rust
351use std::thread;
352
353fn main() {
354 let v = vec![1, 2, 3];
355
356 let handle = thread::spawn(move || {
357 println!("Here's a vector: {:?}", v);
358 });
359
360 handle.join().unwrap();
361}
362```
363
364<span class="caption">Listing 16-5: Using the `move` keyword to force a closure
365to take ownership of the values it uses</span>
366
367What would happen to the code in Listing 16-4 where the main thread called
368`drop` if we use a `move` closure? Would `move` fix that case? Unfortunately,
369no; we would get a different error because what Listing 16-4 is trying to do
370isn’t allowed for a different reason. If we added `move` to the closure, we
371would move `v` into the closure’s environment, and we could no longer call
372`drop` on it in the main thread. We would get this compiler error instead:
373
374```text
375error[E0382]: use of moved value: `v`
376 --> src/main.rs:10:10
377 |
3786 | let handle = thread::spawn(move || {
379 | ------- value moved (into closure) here
380...
38110 | drop(v); // oh no!
382 | ^ value used here after move
383 |
384 = note: move occurs because `v` has type `std::vec::Vec<i32>`, which does
385 not implement the `Copy` trait
386```
387
388Rust’s ownership rules have saved us again! We got an error from the code in
389Listing 16-3 because Rust was being conservative and only borrowing `v` for the
390thread, which meant the main thread could theoretically invalidate the spawned
391thread’s reference. By telling Rust to move ownership of `v` to the spawned
392thread, we’re guaranteeing Rust that the main thread won’t use `v` anymore. If
393we change Listing 16-4 in the same way, we’re then violating the ownership
394rules when we try to use `v` in the main thread. The `move` keyword overrides
395Rust’s conservative default of borrowing; it doesn’t let us violate the
396ownership rules.
397
398With a basic understanding of threads and the thread API, let’s look at what we
399can *do* with threads.