问题
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.
回答1:
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
回答2:
Bringing a 2011 update to this, since I was curious about it too.
Two tickets are currently opened:
- http://redmine.ruby-lang.org/issues/921
- http://jira.codehaus.org/browse/JRUBY-3194
The core devs suggest that require and autoload work in the same manner and are thread safe, in CRuby/JRuby 1.9. This, in the sense that ruby keeps a lock until the file is fully loaded.
This has the inconvenient side-effect of introducing potential deadlocks, however. Specifically:
- Th1 load A and locks it
- Th2 load B and locks it
- Th1 tries to load B as part of loading A, starts waiting for Th2
- Th2 tries to load A as part of loading B, starts waiting for Th1
- Deadlock...
The conclusion probably is: require everything you need before starting a thread if there's any potential for deadlock in your app.
回答3:
it's always broken.
subload allows you to switch modes when in a threaded environment.
I use autoload still in threaded environment, but only during a single threaded boot sequence. I don't see any good reason to have a multithreaded startup process in a real world application. If you do have one, you will likely need to queue up actions for loading shared libraries as you will always have thread safety issues with class and instance level setup, most partuclarly with things like:
class Lib
extend SomeClassFuncs
do_something_with_class_funcs
end
This code is not thread safe a load time, irrespective of the loader.
If you can't see this, you shouldn't be threading.
来源:https://stackoverflow.com/questions/2837912/is-autoload-thread-safe-in-ruby-1-9