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

----------------------------------------------------------------

.



Relevant Pages

  • Re: Mutex locking problem...
    ... I have also tested the mutex object by making it local and trying to lock it ... but it is locking two times.. ... > __declspec (dllexport) void fIncrementSecond ... > have used lock method without unlocking the mt1.. ...
    (microsoft.public.vc.language)
  • Re: LazyLoad
    ... > Both Lazy objects don't share the same Mutex object, so, ... > effectively, under these circumstances, there's no locking. ... the way I would do it is to treat load as a single computation ...
    (comp.lang.ruby)
  • Re: Image.SetPropertyItem does not work
    ... However when I restart the PC, the exception gone... ... Assuming that there's nothing important in the code you left out, we're either back to some issue with the PropertyItem itself, or an OS/.NET version difference. ... You should be able to use that without any additional effort to see whether you see a locking issue on your configuration; if not, then there's something about the specific usage your code has that's causing problems, rather than a general failure with a locked file. ... I have no Windows 7 to test, but if what you said is true then I think it is version-specific issue. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Is Singleton collection of Singletons possible??
    ... >> each call locking all other calls out until it completed. ... > Well, yes maybe so, but then the 2nd thread would get an ArgumentException ... yes, with Add you would get an exception the second time, if you're ... Possibly only the option of not using an indexer. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Open files and locks
    ... Thank you for the response. ... if a file has a lock and throws an ... exception that tells me its locked but not who is locking it. ...
    (microsoft.public.dotnet.languages.csharp)