From 0257e73425fcdb16a3eef8458c305d47a1b055ee Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Sun, 26 Feb 2017 21:03:33 -0500 Subject: [PATCH] Fix headings --- src/ch16-01-threads.md | 8 ++++---- src/ch16-02-message-passing.md | 8 +++----- src/ch16-03-shared-state.md | 4 ++-- src/ch16-04-extensible-concurrency-sync-and-send.md | 8 ++++---- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/ch16-01-threads.md b/src/ch16-01-threads.md index 03c302d74..9f4ff9494 100644 --- a/src/ch16-01-threads.md +++ b/src/ch16-01-threads.md @@ -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: diff --git a/src/ch16-02-message-passing.md b/src/ch16-02-message-passing.md index 4847ef071..978319afe 100644 --- a/src/ch16-02-message-passing.md +++ b/src/ch16-02-message-passing.md @@ -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` 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. diff --git a/src/ch16-03-shared-state.md b/src/ch16-03-shared-state.md index 6f2e606fb..93c224b54 100644 --- a/src/ch16-03-shared-state.md +++ b/src/ch16-03-shared-state.md @@ -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 diff --git a/src/ch16-04-extensible-concurrency-sync-and-send.md b/src/ch16-04-extensible-concurrency-sync-and-send.md index d0a3c0550..549450f40 100644 --- a/src/ch16-04-extensible-concurrency-sync-and-send.md +++ b/src/ch16-04-extensible-concurrency-sync-and-send.md @@ -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: 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` is `Sync`, and so `RefCell` are not `Sync`, and so `Arc>`, for example, would not be `Sync`. -## Building your own abstractions +## Building Your Own Abstractions Should we include crossbeam here?