Fix headings

This commit is contained in:
Carol (Nichols || Goulding)
2017-02-26 21:03:33 -05:00
parent 664957187c
commit 0257e73425
4 changed files with 13 additions and 15 deletions
+4 -4
View File
@@ -1,4 +1,4 @@
# Threads
## Threads
When your program executes, the operating system runs it in the context of
something called a "process." This is, of course, operating system dependent,
@@ -32,7 +32,7 @@ deal with these drawbacks.
Regardless, now that we've covered the basic concepts of threads, let's check
out the API that the standard library gives us.
## spawn
### Creating a New Thread with `spawn`
To create a new thread, we pass a closure to the `thread::spawn` function:
@@ -51,7 +51,7 @@ fn main() {
The code inside the closure will be executed in a new OS thread; when the
function ends, so does the thread.
### join handles
#### Waiting for All Threads to Finish Using `join` Handles
There's one trick though: if the main thread ends, the whole program ends. So
our example program above is not guaranteed to actually execute the new thread.
@@ -92,7 +92,7 @@ fn main() {
We would be guaranteed to see "Hello from a new thread!" before "Hello from the
main thread"; the call to `join` ensures that.
## move closures
### Using `move` Closures with Threads
There's a feature of closures that we didn't cover in Chapter 13 that's often
useful with `thread::spawn`: `move` closures. We said this back then:
+3 -5
View File
@@ -1,4 +1,4 @@
# Message Passing
## Message Passing
One approach to concurrency that's seen a rise in popularity as of late is
*message passing*, where threads or actors communicate by sending each other
@@ -44,7 +44,7 @@ a value is sent down the channel. Once one is, it will return it. The `try_`
variant of `recv` will not block; it instead returns a `Result<T, E>`
immediately.
## Channels and ownership
### Channels and Ownership
These basic aspects of channels are the same in every language: send something
down one end, receive it on the other end. Where Rust is special, though, is
@@ -89,7 +89,7 @@ Message passing enthusiasts enjoy it for similar reasons as Rustaceans enjoy
Rust; single ownership means certain classes of problems go away. If only one
thread at a time can own some memory, there's no chance of getting a data race.
## An example
### Using Channels to Send Data Between Threads
Let's show off a slightly more realistic example. Let's make a thread that
generates numbers. It'll then send the numbers down a channel. The main thread
@@ -328,6 +328,4 @@ fn main() {
}
```
## Summary
Now that we've seen how channels work, let's look at shared-memory concurrency.
+2 -2
View File
@@ -1,4 +1,4 @@
# Shared state
## Shared State Concurrency
While message passing is a fine way of dealing with concurrency, it's not the
only one. Consider the slogan again:
@@ -18,7 +18,7 @@ additional complexity, as we need to manage these different owners somehow.
Ownership can help a lot here, though. For example, let's look at one of the
more common concurrency primitives for shared memory: mutexes.
## Mutexes
### Mutexes Allow Access to One Thread at a Time
A *mutex* is a concurrency primitive for sharing memory. It's short for "mutual
exclusion", that is, it only allows one thread to access some data at any given
@@ -1,4 +1,4 @@
# Extensible Concurrency with `Sync` and `Send`
## Extensible Concurrency with `Sync` and `Send`
One interesting aspect of Rust's concurrency model is that the language knows
_very_ little about concurrency. Almost everything we've been talking about so
@@ -39,7 +39,7 @@ fn takes_marker<T: Marker>(t: T) {
It's the same for `Send` and `Sync`: they don't add any methods, they only
indicate concurrency properties.
## `Send`
### `Send` for Indicating Ownership May Be Transferred
The `Send` marker trait indicates that a type is allowed to have ownership
transferred between threads. Almost every Rust type is `Send`, but there are
@@ -48,7 +48,7 @@ threadsafe in many circumstances. The Rust wrappers for it need to take this
into account. But in pure Rust code, virtually everything is `Send`, so we
won't spend more time on it.
## `Sync`
### `Sync` for Indicating Access from Multiple Threads is Safe
`Sync`, on the other hand, is more interesting. If your type is `Sync`, it's
safe to access from multiple threads. This is why, when we tried to share our
@@ -58,6 +58,6 @@ however, is, as long as its `T` is also `Sync`. `Mutex<T>` is `Sync`, and so
`RefCell<T>` are not `Sync`, and so `Arc<RefCell<T>>`, for example, would not
be `Sync`.
## Building your own abstractions
## Building Your Own Abstractions
Should we include crossbeam here?