X-Git-Url: https://git.ralfj.de/rust-101.git/blobdiff_plain/5fc4267c1ffd642bdaa4e403ccd8e9e12c8c1616..4f92e4d16fbdd96084a09567fc5f8e421c8b2351:/src/part15.rs?ds=sidebyside diff --git a/src/part15.rs b/src/part15.rs index ef5e9b4..e22d507 100644 --- a/src/part15.rs +++ b/src/part15.rs @@ -28,7 +28,7 @@ use std::time::Duration; //@ Rather than giving every field a name, a struct can also be defined by just giving a sequence //@ of types (similar to how a variant of an `enum` is defined). This is called a *tuple struct*. //@ It is often used when constructing a *newtype*, as we do here: `ConcurrentCounter` is -//@ essentially just a new name for `Arc>`. However, is is a locally declared types, +//@ essentially just a new name for `Arc>`. However, it is a locally declared types, //@ so we can give it an inherent implementation and implement traits for it. Since the field is //@ private, nobody outside this module can even know the type we are wrapping. @@ -116,16 +116,15 @@ pub fn main() { // `unwrap_or_else` for this job. //@ ## `RwLock` - -//@ 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 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. +//@ 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 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. // **Exercise 15.3**: Change the code above to use `RwLock`, such that multiple calls to `get` can // be executed at the same time.