Ruby 1.9 中的自动加载是线程安全的吗?
在我看来,自从这个著名的帖子,出于线程安全原因不鼓励使用它。
有谁知道这在 Ruby 1.9.1 或 1.9.2 中是否不再是问题?我见过一些关于在互斥体等中包装 require 的讨论,但是 1.9 变更日志(或者至少是我能找到的那么多)似乎并没有解决这个特定的问题。我想知道我是否可以合理地开始在仅限 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对此进行了 2011 年更新,因为我也对此感到好奇。
目前已开放两票:
核心开发人员建议,在 CRuby/JRuby 1.9 中,require 和 autoload 以相同的方式工作并且是线程安全的。从某种意义上说,Ruby 会一直锁定直到文件完全加载。
然而,这会带来不便的副作用,即引入潜在的死锁。具体来说:
结论可能是:如果您的应用程序中存在死锁的可能性,则在启动线程之前需要您需要的一切。
Bringing a 2011 update to this, since I was curious about it too.
Two tickets are currently opened:
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:
The conclusion probably is: require everything you need before starting a thread if there's any potential for deadlock in your app.
我不知道一般情况,但该线程的重现示例在 1.9.1 中不会中断:
autoloaded.rb:
autoloader.rb:
I don't know about the general case, but repro example from that thread doesn't break in 1.9.1:
autoloaded.rb:
autoloader.rb:
它总是坏掉的。
子加载允许您在线程环境中切换模式。
我仍然在线程环境中使用自动加载,但仅在单线程启动序列期间使用。我认为在现实世界的应用程序中没有任何充分的理由采用多线程启动过程。如果你确实有一个,你可能需要排队加载共享库的操作,因为你总是会遇到类和实例级别设置的线程安全问题,最特别的是:
此代码在加载时不是线程安全的,无论加载时间如何装载机的。
如果你看不到这一点,那么你就不应该进行线程化。
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:
This code is not thread safe a load time, irrespective of the loader.
If you can't see this, you shouldn't be threading.