diff --git a/appendix-00.html b/appendix-00.html index 2dbadb639..6a0ef65c0 100644 --- a/appendix-00.html +++ b/appendix-00.html @@ -47,7 +47,7 @@
diff --git a/appendix-01-keywords.html b/appendix-01-keywords.html index 408da1942..250904179 100644 --- a/appendix-01-keywords.html +++ b/appendix-01-keywords.html @@ -47,7 +47,7 @@
diff --git a/appendix-02-operators.html b/appendix-02-operators.html index b7e699fc6..25c981d77 100644 --- a/appendix-02-operators.html +++ b/appendix-02-operators.html @@ -47,7 +47,7 @@
diff --git a/appendix-03-derivable-traits.html b/appendix-03-derivable-traits.html index f1590e5fa..d031201bf 100644 --- a/appendix-03-derivable-traits.html +++ b/appendix-03-derivable-traits.html @@ -47,7 +47,7 @@
diff --git a/ch01-00-introduction.html b/ch01-00-introduction.html index 32e5b3935..8db62a855 100644 --- a/ch01-00-introduction.html +++ b/ch01-00-introduction.html @@ -47,7 +47,7 @@
diff --git a/ch01-01-installation.html b/ch01-01-installation.html index aec9d7158..439a170c2 100644 --- a/ch01-01-installation.html +++ b/ch01-01-installation.html @@ -47,7 +47,7 @@
diff --git a/ch01-02-hello-world.html b/ch01-02-hello-world.html index a1ac82d40..bb3689c76 100644 --- a/ch01-02-hello-world.html +++ b/ch01-02-hello-world.html @@ -47,7 +47,7 @@
diff --git a/ch02-00-guessing-game-tutorial.html b/ch02-00-guessing-game-tutorial.html index 7d18bd31f..4c31fdde4 100644 --- a/ch02-00-guessing-game-tutorial.html +++ b/ch02-00-guessing-game-tutorial.html @@ -47,7 +47,7 @@
diff --git a/ch03-00-common-programming-concepts.html b/ch03-00-common-programming-concepts.html index cdc42c5bf..2e6e6f28a 100644 --- a/ch03-00-common-programming-concepts.html +++ b/ch03-00-common-programming-concepts.html @@ -47,7 +47,7 @@
diff --git a/ch03-01-variables-and-mutability.html b/ch03-01-variables-and-mutability.html index 6a71451da..67843d46e 100644 --- a/ch03-01-variables-and-mutability.html +++ b/ch03-01-variables-and-mutability.html @@ -47,7 +47,7 @@
diff --git a/ch03-02-data-types.html b/ch03-02-data-types.html index 396309f3c..deeaf6918 100644 --- a/ch03-02-data-types.html +++ b/ch03-02-data-types.html @@ -47,7 +47,7 @@
diff --git a/ch03-03-how-functions-work.html b/ch03-03-how-functions-work.html index d6f5d6ca0..fbc75e9ce 100644 --- a/ch03-03-how-functions-work.html +++ b/ch03-03-how-functions-work.html @@ -47,7 +47,7 @@
diff --git a/ch03-04-comments.html b/ch03-04-comments.html index 8cbaa9753..a055dac4f 100644 --- a/ch03-04-comments.html +++ b/ch03-04-comments.html @@ -47,7 +47,7 @@
diff --git a/ch03-05-control-flow.html b/ch03-05-control-flow.html index 05ef4e4ab..975865ce4 100644 --- a/ch03-05-control-flow.html +++ b/ch03-05-control-flow.html @@ -47,7 +47,7 @@
diff --git a/ch04-00-understanding-ownership.html b/ch04-00-understanding-ownership.html index 2d41deade..52fd13959 100644 --- a/ch04-00-understanding-ownership.html +++ b/ch04-00-understanding-ownership.html @@ -47,7 +47,7 @@
diff --git a/ch04-01-what-is-ownership.html b/ch04-01-what-is-ownership.html index 5cb62fb82..f7fdedd7f 100644 --- a/ch04-01-what-is-ownership.html +++ b/ch04-01-what-is-ownership.html @@ -47,7 +47,7 @@
diff --git a/ch04-02-references-and-borrowing.html b/ch04-02-references-and-borrowing.html index e5659825a..042c5391b 100644 --- a/ch04-02-references-and-borrowing.html +++ b/ch04-02-references-and-borrowing.html @@ -47,7 +47,7 @@
diff --git a/ch04-03-slices.html b/ch04-03-slices.html index 8f51a683f..9b450ba3e 100644 --- a/ch04-03-slices.html +++ b/ch04-03-slices.html @@ -47,7 +47,7 @@
diff --git a/ch05-00-structs.html b/ch05-00-structs.html index 19f731e80..9ce52ed5a 100644 --- a/ch05-00-structs.html +++ b/ch05-00-structs.html @@ -47,7 +47,7 @@
diff --git a/ch05-01-method-syntax.html b/ch05-01-method-syntax.html index 35817607e..4ad1917b0 100644 --- a/ch05-01-method-syntax.html +++ b/ch05-01-method-syntax.html @@ -47,7 +47,7 @@
diff --git a/ch06-00-enums.html b/ch06-00-enums.html index 61dc3ca7c..39cf0852b 100644 --- a/ch06-00-enums.html +++ b/ch06-00-enums.html @@ -47,7 +47,7 @@
diff --git a/ch06-01-option.html b/ch06-01-option.html index dfebe576c..e932190de 100644 --- a/ch06-01-option.html +++ b/ch06-01-option.html @@ -47,7 +47,7 @@
diff --git a/ch06-02-match.html b/ch06-02-match.html index 6fcbfc54b..e8105cba6 100644 --- a/ch06-02-match.html +++ b/ch06-02-match.html @@ -47,7 +47,7 @@
diff --git a/ch06-03-if-let.html b/ch06-03-if-let.html index 0517dbf11..c700ce970 100644 --- a/ch06-03-if-let.html +++ b/ch06-03-if-let.html @@ -47,7 +47,7 @@
diff --git a/ch07-00-modules.html b/ch07-00-modules.html index 60545570e..cca17c3bc 100644 --- a/ch07-00-modules.html +++ b/ch07-00-modules.html @@ -47,7 +47,7 @@
diff --git a/ch07-01-mod-and-the-filesystem.html b/ch07-01-mod-and-the-filesystem.html index 8811f6d28..6a1582edc 100644 --- a/ch07-01-mod-and-the-filesystem.html +++ b/ch07-01-mod-and-the-filesystem.html @@ -47,7 +47,7 @@
diff --git a/ch07-02-controlling-visibility-with-pub.html b/ch07-02-controlling-visibility-with-pub.html index 4e5611930..88b7e97d9 100644 --- a/ch07-02-controlling-visibility-with-pub.html +++ b/ch07-02-controlling-visibility-with-pub.html @@ -47,7 +47,7 @@
diff --git a/ch07-03-importing-names-with-use.html b/ch07-03-importing-names-with-use.html index f76394bc9..92108344c 100644 --- a/ch07-03-importing-names-with-use.html +++ b/ch07-03-importing-names-with-use.html @@ -47,7 +47,7 @@
diff --git a/ch08-00-fundamental-collections.html b/ch08-00-fundamental-collections.html index 114960b55..9cd7a24ae 100644 --- a/ch08-00-fundamental-collections.html +++ b/ch08-00-fundamental-collections.html @@ -47,7 +47,7 @@
diff --git a/ch08-01-vectors.html b/ch08-01-vectors.html index 2fcbcf8a5..90536019f 100644 --- a/ch08-01-vectors.html +++ b/ch08-01-vectors.html @@ -47,7 +47,7 @@
diff --git a/ch08-02-strings.html b/ch08-02-strings.html index 785018334..5b5a8ab8b 100644 --- a/ch08-02-strings.html +++ b/ch08-02-strings.html @@ -47,7 +47,7 @@
diff --git a/ch08-03-hash-maps.html b/ch08-03-hash-maps.html index 2dc32b078..975965fad 100644 --- a/ch08-03-hash-maps.html +++ b/ch08-03-hash-maps.html @@ -47,7 +47,7 @@
diff --git a/ch09-00-error-handling.html b/ch09-00-error-handling.html index 6c8e136dd..9a9b2c254 100644 --- a/ch09-00-error-handling.html +++ b/ch09-00-error-handling.html @@ -47,7 +47,7 @@
diff --git a/ch09-01-unrecoverable-errors-with-panic.html b/ch09-01-unrecoverable-errors-with-panic.html index a16b499b9..0287e1361 100644 --- a/ch09-01-unrecoverable-errors-with-panic.html +++ b/ch09-01-unrecoverable-errors-with-panic.html @@ -47,7 +47,7 @@
diff --git a/ch09-02-recoverable-errors-with-result.html b/ch09-02-recoverable-errors-with-result.html index 7e9ced3c2..a7b556bd1 100644 --- a/ch09-02-recoverable-errors-with-result.html +++ b/ch09-02-recoverable-errors-with-result.html @@ -47,7 +47,7 @@
diff --git a/ch09-03-to-panic-or-not-to-panic.html b/ch09-03-to-panic-or-not-to-panic.html index 50ba4ed65..259d4c0a7 100644 --- a/ch09-03-to-panic-or-not-to-panic.html +++ b/ch09-03-to-panic-or-not-to-panic.html @@ -47,7 +47,7 @@
diff --git a/ch10-00-generics.html b/ch10-00-generics.html index 49fba451b..2805f63c9 100644 --- a/ch10-00-generics.html +++ b/ch10-00-generics.html @@ -47,7 +47,7 @@
diff --git a/ch10-01-syntax.html b/ch10-01-syntax.html index a7bf86cbc..7a7b5947d 100644 --- a/ch10-01-syntax.html +++ b/ch10-01-syntax.html @@ -47,7 +47,7 @@
diff --git a/ch10-02-traits.html b/ch10-02-traits.html index 1742c8b90..bde578e79 100644 --- a/ch10-02-traits.html +++ b/ch10-02-traits.html @@ -47,7 +47,7 @@
diff --git a/ch10-03-lifetime-syntax.html b/ch10-03-lifetime-syntax.html index 473a91bc7..90449ee7b 100644 --- a/ch10-03-lifetime-syntax.html +++ b/ch10-03-lifetime-syntax.html @@ -47,7 +47,7 @@
diff --git a/ch11-00-testing.html b/ch11-00-testing.html index 6a423297c..d81d45088 100644 --- a/ch11-00-testing.html +++ b/ch11-00-testing.html @@ -47,7 +47,7 @@
diff --git a/ch11-01-writing-tests.html b/ch11-01-writing-tests.html index 2fa6c3ec3..2bdf0c948 100644 --- a/ch11-01-writing-tests.html +++ b/ch11-01-writing-tests.html @@ -47,7 +47,7 @@
diff --git a/ch11-02-running-tests.html b/ch11-02-running-tests.html index 8de887d21..04e94455a 100644 --- a/ch11-02-running-tests.html +++ b/ch11-02-running-tests.html @@ -47,7 +47,7 @@
diff --git a/ch11-03-test-organization.html b/ch11-03-test-organization.html index c4d7084c6..41b703f46 100644 --- a/ch11-03-test-organization.html +++ b/ch11-03-test-organization.html @@ -47,7 +47,7 @@
diff --git a/ch18-00-concurrency.html b/ch18-00-concurrency.html new file mode 100644 index 000000000..be8171423 --- /dev/null +++ b/ch18-00-concurrency.html @@ -0,0 +1,177 @@ + + + + + The Rust Programming Language + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + +
+

Concurrency

+

This is a really rough sketch of some ideas that this chapter might cover.

+

From the orange website.

+

So, with Rust, it's more subtle than that. That is, while threading proper +isn't part of the language itself, Rust's type system is structured in such a +way as to make it possible to build those kinds of libraries. In other words, +Rust's focus on aliasability ends up solving these problems.

+

So for example, in that paper, 4.1 is about the problem of concurrent +modifiability. And indeed, it says

+
+

Indeed, under the implementation strategy we outlined above, in which the +compiler is unaware of threads, it is allowed to transform code subject only +to sequential cor- rectness constraints and hence could generate the code +con- taining a race.

+
+

However, in Rust, this re-ordering can't happen: Rust won't let you alias x and +y between two threads without some sort of synchronization primitive. But this +isn't because Rust knows about concurrency, it's because Rust knows about +aliasing. In a sense, Rust-the-language makes this program impossible to +write, but a library re-enables you to write this program. You need unsafe to +do this, but it's all wrapped up inside of the implementation of, for example, +Mutex.

+

From the last part of this section:

+
+

Resolving it essential requires a programming-language-defined and +compiler-respected memory model, simply to ensure that the user and compiler +can agree on when there is a data race.

+
+

We're in agreement here, but the model is built around aliasing, not +concurrency.

+

4.2 is about speculatively executing store instructions. I know less about +this, but again, it's built on the idea of two threads accessing data at the +same time, unsynchronized. This can't happen in Rust due to the aliasing rules.

+

4.3 is about register promotion. This cannot happen in Rust, because you don't +call a function to acquire the lock, then do whatever you want. Mutex hides +the value it's locking inside of itself, unable to be accessed from the +outside, and the call to acquire the lock returns a mutable reference to the +inner data. The call to acquire the lock is the only way to get said reference, +and Rust's aliasing rules will forbid any other kind of access through the +returned reference. So this kind of transformation can't happen in Rust either.

+

Section 5 is about performance. It's true that synchronization primitives are +expensive. Rust can again use unsafe code in a disciplined way to provide safe +concurrent modification, while ruling out data races entirely. For example, +consider a simple map operation. We take an array of integers, and for each +element, add one to it. This is an embarrassingly parallel operation, yet, as +the paper mentions, with a pthreads-style approach to making it safe, one would +need either a single lock around the whole array, which destroys the +concurrency entirely, or some set of more fine-grained locks, which introduce +cost, as well as limiting the amount of concurrency to some degree.

+

But with a small utility function [1], which performs a small (ie, non-atomic) +check at runtime, we can safety split up our array into as many disjoint chunks +as we'd like, and then pass each one off to its own thread, which is free to do +the modification with no more synchronization needed. In fact, libraries like +Rayon can even determine roughly the correct amount for you, if you don't want +to think about it, and it will near-transparently just handle this for you (you +change a call from iter() to par_iter() and you're done).

+

1: https://github.com/rust-lang/rust/blob/f8614c397313db00e4b46...

+

So yeah. I'm in agreement with the paper that the language needs to do some +kind of reasoning, but since aliasing and concurrency are so tightly related, I +would argue that the language could understand only aliasing, not concurrency, +and then library abstractions are sufficient.

+ +
+ + + + + + + + + + +
+ + + + + + + + + +
+ + + + + + + + + + + + diff --git a/chXX-patterns.html b/chXX-patterns.html index 373e21e1f..a26e7a908 100644 --- a/chXX-patterns.html +++ b/chXX-patterns.html @@ -47,7 +47,7 @@
@@ -307,7 +307,7 @@ the 5. In other words, the precedence of if behaves like this:

- @@ -321,7 +321,7 @@ the 5. In other words, the precedence of if behaves like this:

- diff --git a/chYY-YY-documentation.html b/chYY-YY-documentation.html index 99bcbacad..86be25d4e 100644 --- a/chYY-YY-documentation.html +++ b/chYY-YY-documentation.html @@ -47,7 +47,7 @@
diff --git a/chYY-YY-public-api.html b/chYY-YY-public-api.html index bef9ba143..474529a68 100644 --- a/chYY-YY-public-api.html +++ b/chYY-YY-public-api.html @@ -47,7 +47,7 @@
diff --git a/chYY-YY-workspaces.html b/chYY-YY-workspaces.html index 109eed1c0..9f0f78679 100644 --- a/chYY-YY-workspaces.html +++ b/chYY-YY-workspaces.html @@ -47,7 +47,7 @@
@@ -115,7 +115,7 @@ into a file which ends in .crate, but is secretly a tarball.

- @@ -129,7 +129,7 @@ into a file which ends in .crate, but is secretly a tarball.

- diff --git a/index.html b/index.html index f374f07ac..f754323fe 100644 --- a/index.html +++ b/index.html @@ -46,7 +46,7 @@
diff --git a/print.html b/print.html index 1631e86df..9430467b5 100644 --- a/print.html +++ b/print.html @@ -47,7 +47,7 @@
@@ -8644,6 +8644,67 @@ technically precise, but is gibberish to a lot of people. And a "package&qu "one or more crates", which confusingly, gets wrapped up by cargo package into a file which ends in .crate, but is secretly a tarball.

Whew!

+

Concurrency

+

This is a really rough sketch of some ideas that this chapter might cover.

+

From the orange website.

+

So, with Rust, it's more subtle than that. That is, while threading proper +isn't part of the language itself, Rust's type system is structured in such a +way as to make it possible to build those kinds of libraries. In other words, +Rust's focus on aliasability ends up solving these problems.

+

So for example, in that paper, 4.1 is about the problem of concurrent +modifiability. And indeed, it says

+
+

Indeed, under the implementation strategy we outlined above, in which the +compiler is unaware of threads, it is allowed to transform code subject only +to sequential cor- rectness constraints and hence could generate the code +con- taining a race.

+
+

However, in Rust, this re-ordering can't happen: Rust won't let you alias x and +y between two threads without some sort of synchronization primitive. But this +isn't because Rust knows about concurrency, it's because Rust knows about +aliasing. In a sense, Rust-the-language makes this program impossible to +write, but a library re-enables you to write this program. You need unsafe to +do this, but it's all wrapped up inside of the implementation of, for example, +Mutex.

+

From the last part of this section:

+
+

Resolving it essential requires a programming-language-defined and +compiler-respected memory model, simply to ensure that the user and compiler +can agree on when there is a data race.

+
+

We're in agreement here, but the model is built around aliasing, not +concurrency.

+

4.2 is about speculatively executing store instructions. I know less about +this, but again, it's built on the idea of two threads accessing data at the +same time, unsynchronized. This can't happen in Rust due to the aliasing rules.

+

4.3 is about register promotion. This cannot happen in Rust, because you don't +call a function to acquire the lock, then do whatever you want. Mutex hides +the value it's locking inside of itself, unable to be accessed from the +outside, and the call to acquire the lock returns a mutable reference to the +inner data. The call to acquire the lock is the only way to get said reference, +and Rust's aliasing rules will forbid any other kind of access through the +returned reference. So this kind of transformation can't happen in Rust either.

+

Section 5 is about performance. It's true that synchronization primitives are +expensive. Rust can again use unsafe code in a disciplined way to provide safe +concurrent modification, while ruling out data races entirely. For example, +consider a simple map operation. We take an array of integers, and for each +element, add one to it. This is an embarrassingly parallel operation, yet, as +the paper mentions, with a pthreads-style approach to making it safe, one would +need either a single lock around the whole array, which destroys the +concurrency entirely, or some set of more fine-grained locks, which introduce +cost, as well as limiting the amount of concurrency to some degree.

+

But with a small utility function [1], which performs a small (ie, non-atomic) +check at runtime, we can safety split up our array into as many disjoint chunks +as we'd like, and then pass each one off to its own thread, which is free to do +the modification with no more synchronization needed. In fact, libraries like +Rayon can even determine roughly the correct amount for you, if you don't want +to think about it, and it will near-transparently just handle this for you (you +change a call from iter() to par_iter() and you're done).

+

1: https://github.com/rust-lang/rust/blob/f8614c397313db00e4b46...

+

So yeah. I'm in agreement with the paper that the language needs to do some +kind of reasoning, but since aliasing and concurrency are so tightly related, I +would argue that the language could understand only aliasing, not concurrency, +and then library abstractions are sufficient.

Patterns

We've actually used patterns a few times so far: they're used in let statements, in function arguments, and in the match expression. Patterns have