rubymultithreadingautoloadruby-1.9

Is autoload thread-safe in Ruby 1.9?


It seems to me that the Ruby community has been freaking out a little about autoload since this famous thread, discouraging its use for thread safety reasons.

Does anyone know if this is no longer an issue in Ruby 1.9.1 or 1.9.2? I've seen a bit of talk about wrapping requires in mutexes and such, but the 1.9 changelogs (or at least as much as I've been able to find) don't seem to address this particular question. I'd like to know if I can reasonably start autoloading in 1.9-only libraries without any reasonable grief.

Thanks in advance for any insights.


Solution

  • I don't know about the general case, but repro example from that thread doesn't break in 1.9.1:

    autoloaded.rb:

    sleep 1
    Bar::Foo = 1
    

    autoloader.rb:

    module Bar
       autoload :Foo, 'autoloaded.rb'
    end
    
    t1 = Thread.new { Bar::Foo }
    t2 = Thread.new { Bar::Foo }
    t1.join; t2.join