X-Git-Url: https://git.ralfj.de/rust-101.git/blobdiff_plain/5baae0ea037ed642b7fe8975fb3004b29827d5b1..530115b3acbf1d7503b4065f12fd0879708a5c90:/src/part13.rs diff --git a/src/part13.rs b/src/part13.rs index 9f42b82..054a006 100644 --- a/src/part13.rs +++ b/src/part13.rs @@ -13,7 +13,7 @@ use std::sync::Arc; //@ pitfall of concurrent programming: data races. We will see how that works concretely. // Before we come to the actual code, we define a data-structure `Options` to store all the information we need -// to complete the job: Which files to work on, which pattern to look for, and how to output.
+// to complete the job: Which files to work on, which pattern to look for, and how to output. //@ Besides just printing all the matching lines, we will also offer to count them, or alternatively to sort them. #[derive(Clone,Copy)] pub enum OutputMode { @@ -159,15 +159,15 @@ pub fn main() { //@ programs memory safe, and that prevents us from invalidating iterators, also helps secure our multi-threaded code against //@ data races. For example, notice how `read_files` sends a `String` to `filter_lines`. At run-time, only the pointer to //@ the character data will actually be moved around (just like when a `String` is passed to a function with full ownership). However, -//@ `read_files` has to *give up* ownership of the string to perform `send`, to it is impossible for an outstanding borrow to -//@ still be around. After it sent the string to the other side, `read_files` has no pointer into the string content +//@ `read_files` has to *give up* ownership of the string to perform `send`, to it is impossible for the string to still be borrowed. +//@ After it sent the string to the other side, `read_files` has no pointer into the string content //@ anymore, and hence no way to race on the data with someone else. //@ //@ There is a little more to this. Remember the `'static` bound we had to add to `register` in the previous parts, to make //@ sure that the callbacks do not reference any pointers that might become invalid? This is just as crucial for spawning //@ a thread: In general, that thread could last for much longer than the current stack frame. Thus, it must not use //@ any pointers to data in that stack frame. This is achieved by requiring the `FnOnce` closure passed to `thread::spawn` -//@ to be valid for lifetime `'static`, as you can see in [its documentation](http://doc.rust-lang.org/stable/std/thread/fn.spawn.html). +//@ to be valid for lifetime `'static`, as you can see in [its documentation](https://doc.rust-lang.org/stable/std/thread/fn.spawn.html). //@ This avoids another kind of data race, where the thread's access races with the callee deallocating its stack frame. //@ It is only thanks to the concept of lifetimes that this can be expressed as part of the type of `spawn`. @@ -189,4 +189,4 @@ pub fn main() { //@ So if the environment of your closure contains an `Rc`, it won't be `Send`, preventing it from causing trouble. If however every //@ captured variable *is* `Send`, then so is the entire environment, and you are good. -//@ [index](main.html) | [previous](part12.html) | [next](part14.html) +//@ [index](main.html) | [previous](part12.html) | [raw source](workspace/src/part13.rs) | [next](part14.html)