You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: rustbook-en/src/ch16-01-threads.md
+20-20Lines changed: 20 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,7 +17,7 @@ to problems, such as:
17
17
inconsistent order
18
18
- Deadlocks, in which two threads are waiting for each other, preventing both
19
19
threads from continuing
20
-
- Bugs that happen only in certain situations and are hard to reproduce and fix
20
+
- Bugs that only happen in certain situations and are hard to reproduce and fix
21
21
reliably
22
22
23
23
Rust attempts to mitigate the negative effects of using threads, but
@@ -26,19 +26,19 @@ a code structure that is different from that in programs running in a single
26
26
thread.
27
27
28
28
Programming languages implement threads in a few different ways, and many
29
-
operating systems provide an API the language can call for creating new threads.
30
-
The Rust standard library uses a _1:1_ model of thread implementation, whereby a
31
-
program uses one operating system thread per one language thread. There are
32
-
crates that implement other models of threading that make different tradeoffs to
33
-
the 1:1 model. (Rust’s async system, which we will see in the next chapter,
34
-
provides another approach to concurrency as well.)
29
+
operating systems provide an API the programming language can call for creating
30
+
new threads. The Rust standard library uses a _1:1_ model of thread
31
+
implementation, whereby a program uses one operating system thread per one
32
+
language thread. There are crates that implement other models of threading that
33
+
make different trade-offs to the 1:1 model. (Rust’s async system, which we will
34
+
see in the next chapter, provides another approach to concurrency as well.)
35
35
36
36
### Creating a New Thread with `spawn`
37
37
38
38
To create a new thread, we call the `thread::spawn` function and pass it a
39
39
closure (we talked about closures in Chapter 13) containing the code we want to
40
40
run in the new thread. The example in Listing 16-1 prints some text from a main
41
-
thread and other text from a new thread:
41
+
thread and other text from a new thread.
42
42
43
43
<Listingnumber="16-1"file-name="src/main.rs"caption="Creating a new thread to print one thing while the main thread prints something else">
44
44
@@ -88,13 +88,13 @@ the time due to the main thread ending, but because there is no guarantee on
88
88
the order in which threads run, we also can’t guarantee that the spawned thread
89
89
will get to run at all!
90
90
91
-
We can fix the problem of the spawned thread not running or ending prematurely
92
-
by saving the return value of `thread::spawn` in a variable. The return type of
93
-
`thread::spawn` is `JoinHandle<T>`. A `JoinHandle<T>` is an owned value that,
94
-
when we call the `join` method on it, will wait for its thread to finish.
95
-
Listing 16-2 shows how to use the `JoinHandle<T>` of the thread we created in
96
-
Listing 16-1 and how to call `join` to make sure the spawned thread finishes
97
-
before `main` exits.
91
+
We can fix the problem of the spawned thread not running or of it ending
92
+
prematurely by saving the return value of `thread::spawn` in a variable. The
93
+
return type of `thread::spawn` is `JoinHandle<T>`. A `JoinHandle<T>` is an
94
+
owned value that, when we call the `join` method on it, will wait for its
95
+
thread to finish. Listing 16-2 shows how to use the `JoinHandle<T>` of the
96
+
thread we created in Listing 16-1 and how to call `join` to make sure the
97
+
spawned thread finishes before `main` exits.
98
98
99
99
<Listingnumber="16-2"file-name="src/main.rs"caption="Saving a `JoinHandle<T>` from `thread::spawn` to guarantee the thread is run to completion">
100
100
@@ -172,11 +172,11 @@ threads run at the same time.
172
172
173
173
### Using `move` Closures with Threads
174
174
175
-
We'll often use the `move` keyword with closures passed to `thread::spawn`
175
+
We’ll often use the `move` keyword with closures passed to `thread::spawn`
176
176
because the closure will then take ownership of the values it uses from the
177
177
environment, thus transferring ownership of those values from one thread to
178
-
another. In [“Capturing the Environment With Closures”][capture]<!-- ignore-->
179
-
in Chapter 13, we discussed `move` in the context of closures. Now, we’ll
178
+
another. In [“Capturing References or Moving Ownership”][capture]<!-- ignore
179
+
--> in Chapter 13, we discussed `move` in the context of closures. Now we’ll
180
180
concentrate more on the interaction between `move` and `thread::spawn`.
181
181
182
182
Notice in Listing 16-1 that the closure we pass to `thread::spawn` takes no
@@ -209,7 +209,7 @@ tell how long the spawned thread will run, so it doesn’t know whether the
209
209
reference to `v` will always be valid.
210
210
211
211
Listing 16-4 provides a scenario that’s more likely to have a reference to `v`
212
-
that won’t be valid:
212
+
that won’t be valid.
213
213
214
214
<Listingnumber="16-4"file-name="src/main.rs"caption="A thread with a closure that attempts to capture a reference to `v` from a main thread that drops `v`">
215
215
@@ -277,4 +277,4 @@ ownership rules.
277
277
Now that we’ve covered what threads are and the methods supplied by the thread
278
278
API, let’s look at some situations in which we can use threads.
0 commit comments