From: Patrick Hurley Date: 2006-04-22T11:54:14+09:00 Subject: Re: Synchronized object On 4/21/06, David Corbin wrote: > On Friday 21 April 2006 10:47 am, Kroeger, Simon (ext) wrote: > > Dear ruby-talk, > > > > is there a nice, concise way to lock each and > > every method against all other methods of an object? > > > > To elaborate: I want to make sure that only one method of > > a specific object (not class) can run at a each point in > > time regardless of how many threads are using this object. > > > I would hope there is module i just have to include in > > my class definition. > > > > cheers and thanks > > > > Simon > > It would be relatively easy to write a class that uses Monitor and delegates > to specifc object. Thus, much like Java's Collections.synchronizedFoo, you > could have > > Synchronizer.new(foo) that would synchronize ANY object. > > David > > require 'monitor' class AutoMonitor include MonitorMixin def initialize(obj) mon_initialize @obj = obj end def method_missing(sym, *args, &block) synchronize do @obj.send(sym, *args, &block) end end end class Class def monitored_new(*args, &block) AutoMonitor.new(new(*args, &block)) end end Something like this and then you can: f = Foo.monitored_new pth