From: Wyss Clemens Date: 2001-05-19T15:37:44+09:00 Subject: [ruby-talk:15391] PROPOSAL: Java-like method-based synchronization mechanism in Rub y Hi all, the synchronized-call (in class Demo) in the following code-snippet: require �synchronized� class Demo def method1() ... end def method2() ... end synchronized (:test1) end ensures that method1-calls are synchronized, whereas method2-calls are not. Find attached a pure ruby-solution (synchronized.rb): --------- require 'thread' 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 return @__mutex__ end EOS end methods.each { | method | synchronizeMethod(method.to_s()) } end # synchronize(*methods) private def synchronizeMethod(method) # make a "copy" of the original method instance_eval("alias_method(:__#{method}__, :#{method})") # now create the "synchronized" wrapper method module_eval <<-EOS def #{method}(*args) __mutex__().synchronize { #print 'SYNCHRONIZED CALL'; if args == [] then __#{method}__() else __#{method}__(args) end } end EOS end # synchronizeMethod(method) end --------- Note: �synchronized� is to be applied in the same manner as �attr_reader�, �attr_writer�, etc. Any comments, improvements? Something for the RAA? Enjoy the weekend. Clemens