# How write a synchronized class

**URL:** <https://www.ruby-forum.com/t/how-write-a-synchronized-class/167403>\
**Category:** Ruby\
**Created:** [May 7, 2009, 2:38pm UTC](https://www.ruby-forum.com/t/how-write-a-synchronized-class/167403 "2009-05-07T14:38:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gdo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e9a140/32.png) [@gdo](https://www.ruby-forum.com/u/gdo)\
**Post date:** [May 7, 2009, 2:38pm UTC](https://www.ruby-forum.com/t/how-write-a-synchronized-class/167403/1 "2009-05-07T14:38:10Z")

</div>

Hello

How do to write a synchronized class like in java for Ruby

I try with a Mutex but if a method call another method of the same  
instance I obtain a Deadlock.

Thanks  
Guillaume

---

<div class="post-metadata">

**Author:** ![gdo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e9a140/32.png) [@gdo](https://www.ruby-forum.com/u/gdo)\
**Post date:** [May 7, 2009, 4:38pm UTC](https://www.ruby-forum.com/t/how-write-a-synchronized-class/167403/2 "2009-05-07T16:38:00Z")

</div>

On Thu, May 7, 2009 at 6:38 AM, Guillaume dorchies  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello
> 
> How do to write a synchronized class like in java for Ruby
> 
> I try with a Mutex but if a method call another method of the same  
> instance I obtain a Deadlock.

I wrote a reentrant mutex class at one point in time …

> <https://gist.github.com/TwP/108117>

Give it a shot and see if it fixes your deadlock issue.

Blessings,  
TwP

---

<div class="post-metadata">

**Author:** ![gdo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e9a140/32.png) [@gdo](https://www.ruby-forum.com/u/gdo)\
**Post date:** [May 7, 2009, 4:42pm UTC](https://www.ruby-forum.com/t/how-write-a-synchronized-class/167403/3 "2009-05-07T16:42:58Z")

</div>

Thank you It’s exactly that I need

---

<div class="post-metadata">

**Author:** ![gdo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e9a140/32.png) [@gdo](https://www.ruby-forum.com/u/gdo)\
**Post date:** [May 7, 2009, 5:48pm UTC](https://www.ruby-forum.com/t/how-write-a-synchronized-class/167403/4 "2009-05-07T17:48:27Z")

</div>

2009/5/7 Tim P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I wrote a reentrant mutex class at one point in time …
> 
> [reentrant\_mutex.rb · GitHub](http://gist.github.com/108117)
> 
> Give it a shot and see if it fixes your deadlock issue.

Sorry, but you could have saved yourself the effort because it’s part  
of the standard library:

17:45:59 $ ruby19 -r monitor -e ‘m=Monitor.new;m.synchronize {  
m.synchronize { p 1 } }’  
1  
17:46:05 $ ruby19 -r thread -e ‘m=Mutex.new;m.synchronize {  
m.synchronize { p 1 } }’  
internal:prelude:6:in `lock': deadlock; recursive locking (ThreadError) from <internal:prelude>:6:in `synchronize’  
from -e:1:in `block in <main>' from <internal:prelude>:8:in `synchronize’  
from -e:1:in `’  
17:46:17 $ ruby19 -r monitor -e ‘m=Monitor.new;m.synchronize {  
m.synchronize { p 1 } }’  
1  
17:46:23 $

Kind regards

robert
