From: Max Maischein Date: 2001-12-06T20:09:05+09:00 Subject: [ruby-talk:27670] Behaviour of Mutex I have a somewhat philosophical question regarding the behaviour of Mutex. In my opinion, the following program should not raise an exception : require 'thread' # This is the problem lock = Mutex.new p "First synchronize" lock.synchronize { p "Second synchronize" lock.synchronize { p "Two times synchronized" } p "One time synchronized" } p "Done." It raises an exception because a mutex can't be entered twice, even by the same thread (or at least, that's my interpretation of what's happening). I have written a threadsafe Fifo-queue that has this problem, as some of the Fifo-methods call other methods that do their own locking. The solution would be to write another Mutex (say, ThreadMutex) which derives from Mutex, which allows the current owning thread to always pass the lock. But now for the philosophical part : Shouldn't this be the default behaviour of the Mutex class ? The Mutex is the answer to the problem of multiple threads stepping on each other's feet while changing shared data, but a single thread can't step on its own feet in a bad way. Or at least, that's what I think. One region I haven't fully understood yet is continuations, but a lock can't be useful there because of my above example. So, if anyone can enlighten me about the reason for the current Mutex behaviour, or point me to an implementation that does What I Want, or tell me that I should write it, I'm happy :-)) Have fun, -max PS: I'm perfectly happy to contribute my fifo class for peer review - I think that a language that natively supports threads should also have a queue for inter-thread communication :-))