Ruby 1.9 中的自动加载线程安全吗? [英] Is autoload thread-safe in Ruby 1.9?

查看:39
本文介绍了Ruby 1.9 中的自动加载线程安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,自从 这个著名的帖子以来,Ruby 社区一直对自动加载有点担心,出于线程安全的原因不鼓励使用它.

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.

有谁知道这在 Ruby 1.9.1 或 1.9.2 中是否不再是问题?我已经看到一些关于在互斥锁等中包装需要的讨论,但是 1.9 更改日志(或至少我能找到的尽可能多)似乎没有解决这个特定问题.我想知道我是否可以合理地开始在 1.9-only 库中自动加载而没有任何合理的悲伤.

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.9.1 中没有中断:

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

autoloaded.rb:

autoloaded.rb:

sleep 1
Bar::Foo = 1

autoloader.rb:

autoloader.rb:

module Bar
   autoload :Foo, 'autoloaded.rb'
end

t1 = Thread.new { Bar::Foo }
t2 = Thread.new { Bar::Foo }
t1.join; t2.join

这篇关于Ruby 1.9 中的自动加载线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆