DEV Community

Rust Concurrency Explained: A Beginner's Guide to Arc and Mutex

Iñigo Etxaniz on November 17, 2023

Introduction Jumping into Rust programming has been quite a ride. At first, it felt a bit like trying to solve a puzzle, especially when...
Collapse
 
isaacdlyman profile image
Isaac Lyman

This is an excellent article. Thank you.

One question: If RwLock is better than Mutex even in a situation like this where you aren’t doing concurrent reads, when would you use Mutex?

Collapse
 
ietxaniz profile image
Iñigo Etxaniz

You are absolutely right. In this concrete scenario Mutex would be better as we are not doing any concurrent reads. The thing is that I consider this simple example as the building block of more general or complex use cases and by default I usually use read-write locks without thinking too much on it.

Collapse
 
isaacdlyman profile image
Isaac Lyman

Are there any advantages to Mutex, then? Does it use less memory or make deadlocks easier to catch?

Thread Thread
 
ietxaniz profile image
Iñigo Etxaniz • Edited

Don't know about that, but I guess it will be more efficient as it has less things to check... The thing is that only one thread will have access to data when using mutex while it is locked, whilst using RwLock many reads can be done simultaneously. So RwLock has to check if it is write or read lock and what kind of current lock exists.

Collapse
 
judevector profile image
Jude Ndubuisi 🥷🧑‍💻

As someone who just started his Rust learning journey, I love this article. This will help me understand this awesome language alot, am already loving it 😊

Collapse
 
ietxaniz profile image
Iñigo Etxaniz

Hi Jude,

Thanks for your kind words! It's great to hear you're finding the article helpful as we both navigate the exciting journey of learning Rust. Rust can be quite unique at first, but it's a rewarding experience. Let's keep learning together!