# Thread and Mutex in Ruby 1.8.7

**URL:** <https://www.ruby-forum.com/t/thread-and-mutex-in-ruby-1-8-7/167206>\
**Category:** Ruby\
**Created:** [May 5, 2009, 2:35pm UTC](https://www.ruby-forum.com/t/thread-and-mutex-in-ruby-1-8-7/167206 "2009-05-05T14:35:43Z")\
**Posts on this page:** 3\
**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 5, 2009, 2:35pm UTC](https://www.ruby-forum.com/t/thread-and-mutex-in-ruby-1-8-7/167206/1 "2009-05-05T14:35:43Z")

</div>

Hello, in ruby 1.8.7

I want to do something like this:

require ‘thread’  
class Bar  
def initialize(f)  
@f=f  
end

def m\_a #This method isn’t call only by FOO::m\_b  
@f.m\_a  
end  
end

class Foo  
Â def initialize  
Â Â @mutex=Mutex.new  
Â end

Â def m\_a  
Â Â @mutex.synchronize do  
Â Â Â puts “A”  
Â Â end  
Â end

Â def m\_b  
Â Â @mutex.synchronize do  
Â Â Â puts “B”  
b=Bar.new(self)  
b.m\_a  
Â Â end  
Â end  
end

f=Foo.new  
f.m\_b

I must have

> A  
> B

but I have  
B  
test.rb:8:in `synchronize': stopping only thread (ThreadError) Â Â Â Â note: use sleep to stop forever Â Â Â Â from test.rb:8:in`m\_a’  
Â Â Â Â from test.rb:16:in `m_b' Â Â Â Â from test.rb:14:in`synchronize’  
Â Â Â Â from test.rb:14:in `m\_b’  
Â Â Â Â from test.rb:22

because I can’t do

mutex=Mutex.new  
mutex.synchronize do  
mutex.synchronize do  
puts “A”  
puts “B”  
end  
end

Nevertheless my thread have a lock on the mutex. How to reuse it ?

Thanks

---

<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 9, 2009, 2:33pm UTC](https://www.ruby-forum.com/t/thread-and-mutex-in-ruby-1-8-7/167206/2 "2009-05-09T14:33:00Z")

</div>

On 05.05.2009, at 14:35, Guillaume Gdo wrote:

> mutex=Mutex.new  
> mutex.synchronize do  
> mutex.synchronize do  
> puts “A”  
> puts “B”  
> end  
> end

Mutex is not reentrant.

irb\> m=Mutex.new  
irb\> m.synchronize do  
irb\> puts “foo”  
irb\> m.synchronize do  
irb\> puts “bar”  
irb\> end  
irb\> end  
foo  
ThreadError: thread 0x341ac tried to join itself  
from (irb):4:in `synchronize' from (irb):4 from (irb):2:in`synchronize’  
from (irb):2

> Nevertheless my thread have a lock on the mutex. How to reuse it ?

Use Monitor instead of Mutex.

irb\> m=Monitor.new  
irb\> m.synchronize do  
irb\> puts “foo”  
irb\> m.synchronize do  
irb\> puts “reentrant”  
irb\> end  
irb\> end  
foo  
reentrant

hth. Sandor  
Szücs

---

<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 10, 2009, 2:10pm UTC](https://www.ruby-forum.com/t/thread-and-mutex-in-ruby-1-8-7/167206/3 "2009-05-10T14:10:51Z")

</div>

On 09.05.2009 14:32, Sandor Szücs wrote:

> Mutex is not reentrant.  
> from (irb):4:in `synchronize’  
> irb\> m=Monitor.new  
> irb\> m.synchronize do  
> irb\> puts “foo”  
> irb\> m.synchronize do  
> irb\> puts “reentrant”  
> irb\> end  
> irb\> end  
> foo  
> reentrant

And then there is also MonitorMixin which adds monitor functionality to  
every class or instance.

Kind regards

robert
