1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2024-12-12 05:24:12 +02:00

Remove unnecessary Mutex::lock from shared state example (#499)

It's not necessary to explicitly take the lock to print it.
This commit is contained in:
Oliver Mannion 2023-03-12 20:48:52 +11:00 committed by GitHub
parent 59d3d7f625
commit 9ee562c267
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -42,10 +42,7 @@ fn main() {
handle.join().unwrap(); handle.join().unwrap();
{ println!("v: {v:?}");
let v = v.lock().unwrap();
println!("v: {v:?}");
}
} }
``` ```
@ -55,6 +52,5 @@ Notable parts:
* Wrapping a `Mutex` in an `Arc` is a common pattern to share mutable state between threads. * Wrapping a `Mutex` in an `Arc` is a common pattern to share mutable state between threads.
* `v: Arc<_>` needs to be cloned as `v2` before it can be moved into another thread. Note `move` was added to the lambda signature. * `v: Arc<_>` needs to be cloned as `v2` before it can be moved into another thread. Note `move` was added to the lambda signature.
* Blocks are introduced to narrow the scope of the `LockGuard` as much as possible. * Blocks are introduced to narrow the scope of the `LockGuard` as much as possible.
* We still need to acquire the `Mutex` to print our `Vec`.
</details> </details>