From: Gennady Date: 2004-05-28T02:01:37+09:00 Subject: Re: Mutex re-entrancy You may want to use monitor.rb instead (from the standard ruby distribution), it is designed to be reentrant, while Mutex is not. Gennady. Martin Hart wrote: > Hi all, > > I am using a Mutex to protect a function, but I need to call into the > function again from the same thread without deadlocking: > > class A > def initialize > @m = Mutex.new > end > def a > @m.synchronize { b } > end > def b > @m.synchronize { puts "hello" } > end > end > A.new.a #-> deadlock > > It seems to me that to achieve this, all i need to do is track which thread > has the mutex locked, and allow access if the same thread requests it again: > > class Mutex2 > def initialize > @m = Mutex.new > @l = nil > end > def synchronize > if @l == Thread.current > yield > else > @m.synchronize { > @l = Thread.current > begin > yield > ensure > @l = nil > end > } > end > end > end > > q1) am i reinventing the wheel and/or making it square? > q2) is this safe? > > TIA > Martin > >