From: Erik Veenstra Date: 2006-01-25T10:13:12+09:00 Subject: Re: LazyLoad > On the other end of things -- one issue that I've still got > to address for myself in lazy.rb is threadsafety. So you've > improved on mine in that respect. Well, there is a problem with my locking. Do you remember that a set both Branch@items and Branch@snapshots in Branch#load, like this?: @snapshots = lazy {load; @snapshots} @items = lazy {load; @items} Both Lazy objects don't share the same Mutex object, so, effectively, under these circumstances, there's no locking. So we can get of those parms thing and introduce an external Mutex object: mutex = Mutex.new @snapshots = lazy(mutex) {load; @snapshots} @items = lazy(mutex) {load; @items} Thoughts? gegroet, Erik V. - http://www.erikveen.dds.nl/ ---------------------------------------------------------------- require "thread" module EV class Lazy instance_methods.each do |method| undef_method(method) unless method =~ /^__/ end def initialize(mutex=Mutex.new, &block) @mutex = mutex @block = block @mutex = Mutex.new @evaluated = false @exception = nil @real_object = nil end def method_missing(method_name, *parms, &block) @mutex.synchronize do begin @real_object = @block.call() unless @evaluated rescue Exception => e @exception = e ensure @evaluated = true end end raise @exception if @exception @real_object.send(method_name, *parms, &block) end end end module Kernel def lazy(*parms, &block) EV::Lazy.new(*parms, &block) end end ----------------------------------------------------------------