Skip to content

Commit

Permalink
Move transfers between threads, not shares. Fixes #2843.
Browse files Browse the repository at this point in the history
  • Loading branch information
carols10cents committed Jan 27, 2022
1 parent 95b5e7c commit 72c6960
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/ch16-01-threads.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,12 @@ threads run at the same time.

### Using `move` Closures with Threads

The `move` closure is often used alongside `thread::spawn` because it allows
you to use data from one thread in another thread.

In Chapter 13, we mentioned we can use the `move` keyword before the parameter
list of a closure to force the closure to take ownership of the values it uses
in the environment. This technique is especially useful when creating new
threads in order to transfer ownership of values from one thread to another.
The `move` keyword is often used with closures passed to `thread::spawn`
because the closure will then take ownership of the values it uses from the
environment, thus transferring ownership of those values from one thread to
another. In the [“Capturing the Environment with Closures”][capture]<!-- ignore
--> section of Chapter 13, we discussed `move` in the context of closures. Now,
we’ll concentrate more on the interaction between `move` and `thread::spawn`

Notice in Listing 16-1 that the closure we pass to `thread::spawn` takes no
arguments: we’re not using any data from the main thread in the spawned
Expand Down Expand Up @@ -308,3 +307,5 @@ ownership rules.

With a basic understanding of threads and the thread API, let’s look at what we
can *do* with threads.

[capture]: ch13-01-closures.html#capturing-the-environment-with-closures

0 comments on commit 72c6960

Please sign in to comment.