# What's a Mutex and how do I use it?

**URL:** <https://www.ruby-forum.com/t/whats-a-mutex-and-how-do-i-use-it/103047>\
**Category:** Ruby\
**Created:** [May 1, 2007, 6:45am UTC](https://www.ruby-forum.com/t/whats-a-mutex-and-how-do-i-use-it/103047 "2007-05-01T06:45:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 1, 2007, 6:45am UTC](https://www.ruby-forum.com/t/whats-a-mutex-and-how-do-i-use-it/103047/1 "2007-05-01T06:45:27Z")

</div>

Mutex seems very powerful and something I could use eventually as I  
better my understanding of ruby. Thus far, I haven’t found the  
documentation verbose enough for me to grasp the concept of it. Could  
anyone try their hand at writing a piece (or a reply) that would help  
totally demistify this thing called a Mutex? Maybe a simple use case  
scenario or maybe just 2 or 3 different ways of using it… that would  
be enough for me to understand. Thanks!

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 1, 2007, 9:56am UTC](https://www.ruby-forum.com/t/whats-a-mutex-and-how-do-i-use-it/103047/2 "2007-05-01T09:56:37Z")

</div>

On Tue, May 01, 2007 at 01:45:10PM +0900, removed\_email\_address@domain.invalid wrote:

> Mutex seems very powerful and something I could use eventually as I  
> better my understanding of ruby. Thus far, I haven’t found the  
> documentation verbose enough for me to grasp the concept of it. Could  
> anyone try their hand at writing a piece (or a reply) that would help  
> totally demistify this thing called a Mutex? Maybe a simple use case  
> scenario or maybe just 2 or 3 different ways of using it… that would  
> be enough for me to understand. Thanks!

[http://www.rubycentral.com/book/tut\_threads.html](http://www.rubycentral.com/book/tut_threads.html)

There’s a section headed “Mutual Exclusion”

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 1, 2007, 10:27pm UTC](https://www.ruby-forum.com/t/whats-a-mutex-and-how-do-i-use-it/103047/3 "2007-05-01T22:27:53Z")

</div>

On 5/1/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Mutex seems very powerful and something I could use eventually as I  
> better my understanding of ruby. Thus far, I haven’t found the  
> documentation verbose enough for me to grasp the concept of it. Could  
> anyone try their hand at writing a piece (or a reply) that would help  
> totally demistify this thing called a Mutex? Maybe a simple use case  
> scenario or maybe just 2 or 3 different ways of using it… that would  
> be enough for me to understand. Thanks!

Hi, Mutex comes for Mutual Exclusion. This means that 2 or more  
elements (process, threads, whatever) is trying to take the control or  
use a resource, and this may cause an error because its a called  
“critical section”.

So the class Mutex gives you the lock for you to protect that resource  
when doing concurrent programming.

Hope you understand me, im not the greatest teacher.

More information @: [Mutual exclusion - Wikipedia](http://en.wikipedia.org/wiki/Mutual_exclusion)

Easy examples @: [http://www.rubycentral.com/book/tut\_threads.html](http://www.rubycentral.com/book/tut_threads.html)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 2, 2007, 6:40pm UTC](https://www.ruby-forum.com/t/whats-a-mutex-and-how-do-i-use-it/103047/4 "2007-05-02T18:40:49Z")

</div>

On May 1, 4:58 pm, MenTaLguY [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Hi, Mutex comes for Mutual Exclusion. This means that 2 or more  
> > -mental  
> > Thank you all. And thanks Mental!

So I figure this is ideal if i’m fetching data from different sources  
with different threads but writting them to the same a sqlite  
database… great!

And I must admit, I feel a little stupid now that I realize this pas  
in the pickaxe, but thanks nevertheless!

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 1, 2007, 10:58pm UTC](https://www.ruby-forum.com/t/whats-a-mutex-and-how-do-i-use-it/103047/5 "2007-05-01T22:58:57Z")

</div>

On Wed, 2 May 2007 05:27:18 +0900, “Emilio T.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi, Mutex comes for Mutual Exclusion. This means that 2 or more  
> elements (process, threads, whatever) is trying to take the control or  
> use a resource, and this may cause an error because its a called  
> “critical section”.

Specifically, things can get very confused if one thread accesses or  
modifies an object at the same time another thread is modifying it. To  
prevent such situations, a mutex can be employed.

A mutex is designed so that only one thread can “own” it at a time. If  
we associate a mutex with an object, and we ensure that each thread  
acquires the mutex when it starts using the object and releases it again  
when it is done, then we can guarantee that two threads never access the  
object at the same time, and problems are avoided.

However, rather than openly sharing objects between threads and  
protecting them with mutexes, it is often a better idea to communicate  
between threads using message queues. (the Queue class in thread.rb)  
You can put an object on a thread’s queue when that thread needs to work  
with it, and it can then send any needed objects back using another  
queue when it is done.

-mental
