# Fearless Concurrency Ep.5: Ensuring Memory Safety with Mutexes and RwLocks in Rust

- Published: November 8, 2024
- Author: Ardan Labs
- URL: https://www.ardanlabs.com/blog/2024/11/fearless-concurrency-ep5-ensuring-memory-safety-with-mutexes-and-rwlocks-in-rust.html

### Introduction:

Welcome to Episode 5 of *Fearless Concurrency in Rust*! In this episode, Herbert Wolverson dives into advanced concurrency tools, focusing on `RwLock` for managing simultaneous reads and writes and the powerful concept of interior mutability. These tools provide developers with greater control over data sharing in multithreaded environments, enabling them to optimize for both performance and code simplicity in Rust.

- **Read-Write Locks (`RwLock`):** Manage concurrent read and write access efficiently.

- **Interior Mutability and the `Sync` Trait:** Integrate mutexes within structs for simplified data access.

- **Concurrency Best Practices:** Avoid deadlocks and streamline code for improved thread safety.

![](../../../images/blog/fearless-rust.jpg?v=1)

The episode starts by introducing `RwLock`, Rust’s built-in mechanism for managing read and write access to shared data. With `RwLock`, developers can allow multiple threads to read data at once or restrict access to a single writer when data modifications are required. Herbert explains that while `RwLock` can be useful, especially for cases where data is frequently read and rarely written, it can also lead to performance bottlenecks if used improperly. Write operations must wait until all active readers are done, so Herbert advises limiting `RwLock` use to scenarios where this read-heavy access pattern is a known requirement. He suggests starting with basic mutexes to master synchronization fundamentals before moving to `RwLock` to avoid accidental deadlocks.

In the latter half of the episode, Herbert explores interior mutability and the `Sync` trait, which enable mutexes to be embedded directly within data structures. This approach, known as interior mutability, allows developers to safely manage data across threads without exposing direct locking logic to users of the struct. By embedding a mutex inside a struct, only the struct itself needs to manage synchronization, simplifying access for other parts of the code. Herbert explains how interior mutability can improve concurrency by allowing different threads to lock only the specific parts of the data they need. This way, unnecessary serialization of access to other parts of the struct is avoided, leading to better performance and cleaner code. He walks through examples that showcase how interior mutability, paired with the `Sync` trait, simplifies complex concurrency patterns, making Rust a safer and more productive choice for multithreaded programming.

---

### Video

<div class="post-video-container">
    <iframe
        width="640"
        height="480"
        src="https://www.youtube.com/embed/pXuZ2D27ILY"
        frameborder="0"
        allow="autoplay; encrypted-media"
        allowfullscreen
    >
    </iframe>
</div>
