From: Wyss Clemens Date: 2001-05-20T01:37:00+09:00 Subject: [ruby-talk:15400] Re: PROPOSAL: Java-like method-based synchr onization mechanism in Rub y >> class Module >> def synchronized(*methods) >> if methods.size() > 0 then >> # we need a Mutex (-getter) >> module_eval <<-EOS >> def __mutex__() >> if (@__mutex__.nil?()) then >> @__mutex__ = Mutex.new() >> end >Is this thread safe, or could two different threads each generate >their own mutex? You are right, we need to apply the "Double-Checked Locking" as described in POSA2 ;-) class Module def synchronized(*methods) if methods.size() > 0 then instance_eval("@@__dcl_mutex__ = Mutex.new()") # we need a Mutex (-getter) module_eval <<-EOS def __mutex__() if (@__mutex__.nil?()) then @@__dcl_mutex__.synchronize { if (@__mutex__.nil?()) then @__mutex__ = Mutex.new() end } end return @__mutex__ end EOS end methods.each { | method | synchronizeMethod(method.to_s()) } end # synchronize(*methods) >> Note: �synchronized� is to be applied in the same manner as �attr_reader�, >> �attr_writer�, etc. >I wonder if it should be more like 'protected', 'private', and >'public'? That is, it can apply to a list of methods, or to all >subsequent methods. Not sure. Best would be if we could use "def_synchronized" or "def synchronized" (or...) to denote synchronized methods. But this would require changes to the ruby interpreter. Clemens