X-Git-Url: https://git.ralfj.de/rust-101.git/blobdiff_plain/a2eeb1b93e8f52b2119fb11d56f5ffc764ac747b..85f1f88dad52d7679bf5c1a5f5f6ab8d55252c33:/src/part15.rs?ds=inline diff --git a/src/part15.rs b/src/part15.rs index ef2564a..f60a481 100644 --- a/src/part15.rs +++ b/src/part15.rs @@ -3,6 +3,7 @@ use std::sync::{Arc, Mutex}; use std::thread; +use std::time::Duration; //@ We already saw that we can use `Arc` to share memory between threads. However, `Arc` can only provide *read-only* //@ access to memory: Since there is aliasing, Rust cannot, in general, permit mutation. To implement shared-memory @@ -11,7 +12,7 @@ use std::thread; //@ 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). +//@ The most basic type for interior mutability that supports concurrency is [`Mutex`](https://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 //@ through the mutex. In other words, "lock data, not code" is actually enforced by the type system, which @@ -45,7 +46,7 @@ impl ConcurrentCounter { *counter = *counter + by; //@ At the end of the function, `counter` is dropped and the mutex is available again. //@ This can only happen when full ownership of the guard is given up. In particular, it is impossible for us - //@ to borrow some of its content, release the lock of the mutex, and subsequently access the protected data without holding + //@ to take a reference to some of its content, release the lock of the mutex, and subsequently access the protected data without holding //@ the lock. Enforcing the locking discipline is expressible in the Rust type system, so we don't have to worry //@ about data races *even though* we are mutating shared memory! //@ @@ -70,7 +71,7 @@ pub fn main() { let counter1 = counter.clone(); let handle1 = thread::spawn(move || { for _ in 0..10 { - thread::sleep_ms(15); + thread::sleep(Duration::from_millis(15)); counter1.increment(2); } }); @@ -79,14 +80,14 @@ pub fn main() { let counter2 = counter.clone(); let handle2 = thread::spawn(move || { for _ in 0..10 { - thread::sleep_ms(20); + thread::sleep(Duration::from_millis(20)); counter2.increment(3); } }); // Now we watch the threads working on the counter. for _ in 0..50 { - thread::sleep_ms(5); + thread::sleep(Duration::from_millis(5)); println!("Current value: {}", counter.get()); } @@ -103,9 +104,9 @@ pub fn main() { // the data inside the lock. Change the code above to do that. Try using `unwrap_or_else` for this job. //@ ## `RwLock` -//@ Besides `Mutex`, there's also [`RwLock`](http://doc.rust-lang.org/stable/std/sync/struct.RwLock.html), which +//@ Besides `Mutex`, there's also [`RwLock`](https://doc.rust-lang.org/stable/std/sync/struct.RwLock.html), which //@ provides two ways of locking: One that grants only read-only access, to any number of concurrent readers, and another one -//@ for exclusive write access. Notice that this is the same pattern we already saw with shared vs. mutable borrows. Hence +//@ for exclusive write access. Notice that this is the same pattern we already saw with shared vs. mutable references. Hence //@ another way of explaining `RwLock` is to say that it is like `RefCell`, but works even for concurrent access. Rather than //@ panicking when the data is already borrowed, `RwLock` will of course block the current thread until the lock is available. //@ In this view, `Mutex` is a stripped-down version of `RwLock` that does not distinguish readers and writers. @@ -118,30 +119,30 @@ pub fn main() { //@ `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) -//@ the contents of two mutably borrowed values. So in terms of concurrency, sending a mutable borrow is very much like +//@ talk about the question whether a reference 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`](https://doc.rust-lang.org/stable/std/mem/fn.swap.html) +//@ the contents of two mutable references. So in terms of concurrency, sending a mutable, unique reference is very much like //@ sending full ownership, in the sense that it can be used to move the object to another thread. //@ -//@ But what about `&T`, a shared borrow? Without interior mutability, it would always be all-right to send such values. +//@ But what about `&T`, a shared reference? Without interior mutability, it would always be all-right to send such values. //@ After all, no mutation can be performed, so there can be as many threads accessing the data as we like. In the //@ presence of interior mutability though, the story gets more complicated. Rust introduces another marker trait for //@ this purpose: `Sync`. A type `T` is `Sync` if and only if `&T` is `Send`. Just like `Send`, `Sync` has a default implementation //@ and is thus automatically implemented for a data-structure *if* all its members implement it. //@ -//@ Since `Arc` provides multiple threads with a shared borrow of its content, `Arc` is only `Send` if `T` is `Sync`. +//@ Since `Arc` provides multiple threads with a shared reference to its content, `Arc` is only `Send` if `T` is `Sync`. //@ So if we had used `RefCell` above, which is *not* `Sync`, Rust would have caught that mistake. Notice however that //@ `RefCell` *is* `Send`: If ownership of the entire cell is moved to another thread, it is still not possible for several //@ threads to try to access the data at the same time. //@ -//@ Almost all the types we saw so far are `Sync`, with the exception of `Rc`. Remember that a shared borrow is good enough -//@ for cloning, and we don't want other threads to clone our local `Rc`, so it must not be `Sync`. The rule of `Mutex` -//@ is to enforce synchronization, so it should not be entirely surprising that `Mutex` is `Send` *and* `Sync` provided that -//@ `T` is `Send`. +//@ Almost all the types we saw so far are `Sync`, with the exception of `Rc`. Remember that a shared reference is good enough +//@ for cloning, and we don't want other threads to clone our local `Rc` (they would race for updating the reference count), +//@ so it must not be `Sync`. The rule of `Mutex` is to enforce synchronization, so it should not be entirely surprising that +//@ `Mutex` is `Send` *and* `Sync` provided that `T` is `Send`. //@ //@ You may be curious whether there is a type that's `Sync`, but not `Send`. There are indeed rather esoteric examples //@ of such types, but that's not a topic I want to go into. In case you are curious, there's a //@ [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) | [raw source](https://www.ralfj.de/git/rust-101.git/blob_plain/HEAD:/workspace/src/part15.rs) | [next](part16.html)