You've already forked comprehensive-rust
mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-06-15 22:00:26 +02:00
Update mutex.md (#256)
* Update mutex.md Adding speaker notes why Rust `Mutex` has its design and mentioning briefly `RwLock`. Someone from the audience can notice `unwrap()` in the code, it might be worth to have the answer in speaker notes. * Apply suggestions from code review Co-authored-by: Martin Geisler <martin@geisler.net>
This commit is contained in:
@ -26,3 +26,15 @@ implementation.
|
||||
[1]: https://doc.rust-lang.org/std/sync/struct.Mutex.html
|
||||
[2]: https://doc.rust-lang.org/std/sync/struct.Mutex.html#impl-Sync-for-Mutex%3CT%3E
|
||||
[3]: https://doc.rust-lang.org/std/sync/struct.Arc.html
|
||||
|
||||
<details>
|
||||
|
||||
* `Mutex` in Rust looks like a collection with just one element - the protected data.
|
||||
* It is not possible to forget to acquire the mutex before accessing the protected data.
|
||||
* A read-write lock counterpart - `RwLock`.
|
||||
* Why does `lock()` return a `Result`?
|
||||
* If the thread that held the `Mutex` panicked, the `Mutex` becomes "poisoned" to signal that the data it protected might be in an inconsistent state. Calling `lock()` on a poisoned mutex fails with a [`PoisonError`]. You can call `into_inner()` on the error to recover the data regardless.
|
||||
|
||||
[`PoisonError`]: https://doc.rust-lang.org/std/sync/struct.PoisonError.html
|
||||
|
||||
</details>
|
||||
|
Reference in New Issue
Block a user