# Mutex object

**URL:** <https://www.ruby-forum.com/t/mutex-object/184702>\
**Category:** Ruby\
**Created:** [March 7, 2010, 2:07am UTC](https://www.ruby-forum.com/t/mutex-object/184702 "2010-03-07T02:07:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ash34](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9de053/32.png) [@ash34](https://www.ruby-forum.com/u/ash34)\
**Post date:** [March 7, 2010, 2:07am UTC](https://www.ruby-forum.com/t/mutex-object/184702/1 "2010-03-07T02:07:28Z")

</div>

Should I create a new Mutex object per thread accessing a shared  
resource or is it per shared resource being accessed. I understand that  
I need to create a Mutex object and use the synchronize method to make  
the associated block appear as an atomic operation but not sure how to  
decide when and how many Mutex objects need to be created. Can someone  
explain.

thanks.

---

<div class="post-metadata">

**Author:** ![ash34](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9de053/32.png) [@ash34](https://www.ruby-forum.com/u/ash34)\
**Post date:** [March 8, 2010, 2:40am UTC](https://www.ruby-forum.com/t/mutex-object/184702/2 "2010-03-08T02:40:28Z")

</div>

You need one mutex by resource. The Mutex will control the access to  
that object, giving exclusive permission for a thread to modify it.  
If you have a list and a hash, for example, that will be shared by  
three threads, you’ll need two mutexes: one for the list, and one for  
the hash. While a thread modifies the list, others can modify the  
hash, and so on. It’s a good practice to always synchronise multiple  
mutexes in the same order, to prevent deadlocks.

---

<div class="post-metadata">

**Author:** ![ash34](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9de053/32.png) [@ash34](https://www.ruby-forum.com/u/ash34)\
**Post date:** [March 8, 2010, 12:46pm UTC](https://www.ruby-forum.com/t/mutex-object/184702/3 "2010-03-08T12:46:15Z")

</div>

2010/3/8 p0Eta [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> You need one mutex by resource. The Mutex will control the access to  
> that object, giving exclusive permission for a thread to modify it.  
> If you have a list and a hash, for example, that will be shared by  
> three threads, you’ll need two mutexes: one for the list, and one for  
> the hash.

This is only true if the Array and the Hash are supposed to be  
manipulated independently. If they need to be changed in sync, you  
need a single mutex.

> While a thread modifies the list, others can modify the  
> hash, and so on. It’s a good practice to always synchronise multiple  
> mutexes in the same order, to prevent deadlocks.

… if you need to hold more than one lock, yes.

OP, you need one mutex or monitor per critical section. How many  
objects you manipulate in that critical section is totally dependent  
on the logic to execute. If you allocate mutexes per thread, you do  
not have mutexes (“mutual exclusion”) - assuming each thread gets its  
own mutex.

Kind regards

robert
