X-Git-Url: https://git.ralfj.de/rust-101.git/blobdiff_plain/5baae0ea037ed642b7fe8975fb3004b29827d5b1..126f7bfe68883e6465d42c529b00d797b2f8478a:/src/part15.rs diff --git a/src/part15.rs b/src/part15.rs index 9ae5aaf..99eb3be 100644 --- a/src/part15.rs +++ b/src/part15.rs @@ -10,6 +10,7 @@ use std::thread; //@ there are no data races. In Rust, shared-memory concurrency is obtained through *interior mutability*, //@ which we already discussed in a single-threaded context in part 12. //@ +//@ ## `Mutex` //@ The most basic type for interior mutability that supports concurrency is [`Mutex`](http://doc.rust-lang.org/stable/std/sync/struct.Mutex.html). //@ This type implements *critical sections* (or *locks*), but in a data-driven way: One has to specify //@ the type of the data that's protected by the mutex, and Rust ensures that the data is *only* accessed @@ -111,14 +112,14 @@ pub fn main() { // **Exercise 15.3**: Change the code above to use `RwLock`, such that multiple calls to `get` can be executed at the same time. -//@ ## Sync +//@ ## `Sync` //@ Clearly, if we had used `RefCell` rather than `Mutex`, the code above could not work: `RefCell` is not prepared for //@ multiple threads trying to access the data at the same time. How does Rust make sure that we don't accidentally use //@ `RefCell` across multiple threads? //@ //@ In part 13, we talked about types that are marked `Send` and thus can be moved to another thread. However, we did *not* //@ talk about the question whether a borrow is `Send`. For `&mut T`, the answer is: It is `Send` whenever `T` is send. -//@ `&mut` allows moving values back and forth, it is even possible to [`swap`](http://doc.rust-lang.org/beta/std/mem/fn.swap.html) +//@ `&mut` allows moving values back and forth, it is even possible to [`swap`](http://doc.rust-lang.org/stable/std/mem/fn.swap.html) //@ the contents of two mutably borrowed values. So in terms of concurrency, sending a mutable borrow is very much like //@ sending full ownership, in the sense that it can be used to move the object to another thread. //@ @@ -143,4 +144,4 @@ pub fn main() { //@ [Rust RFC](https://github.com/rust-lang/rfcs/blob/master/text/0458-send-improvements.md), which contains a type `RcMut` that would be `Sync` and not `Send`. //@ You may also be interested in [this blog post](https://huonw.github.io/blog/2015/02/some-notes-on-send-and-sync/) on the topic. -//@ [index](main.html) | [previous](part14.html) | [next](main.html) +//@ [index](main.html) | [previous](part14.html) | [next](part16.html)