From: jason@... (Jason Voegele) Date: 2001-11-07T06:26:43+09:00 Subject: [ruby-talk:24462] Is Mutex Reentrant? I've got a scenario in which several objects are accessing the same resource, and access needs to be made thread-safe. The resource they are using exposes a synchronize method that looks like this: def synchronize if block_given? @mutex.synchronize { yield } end end Each object that uses this resource must do so by passing a block to the synchronize method. Unfortunately, even when only a single thread is active, I get the following error when synchronize calls are nested: /c/ruby/lib/ruby/1.6/thread.rb:59:in `stop'(StorageTest): stopping only thread note: use sleep to stop forever (ThreadError) from /c/ruby/lib/ruby/1.6/thread.rb:59:in `lock' from /c/ruby/lib/ruby/1.6/thread.rb:85:in `synchronize' from ./protocol.rb:138:in `synchronize' .... By nested calls, I mean that there's a scenario that looks like this: class NestedCalls def foo @resource.synchronize { @resource.do_something self.bar } end def bar @resource.synchronize { @resource.do_something self.do_other_things } end end I would have thought that mutexes would be reentrant, meaning that a thread could call synchronize multiple times and not interfere with itself. So, my question is, are mutexes reentrant? If so, what have I done wrong? Thanks, Jason Voegele