Ruby /lib/time.rb 如何线程安全?
How is Ruby /lib/time.rb thread-safe?
在查看 RSS stdlib monkey patching the Time 类 时,我发现全局变量 </code> <code>
</code> ... 在那里被大量使用。好吧,我可以不用同时制作多个 RSS 提要。</p>
<p>但我在这里发现了同样的恐怖:<br>
- /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0//time.rb<br>
这里:<br>
- <a href="https://github.com/ruby/ruby/blob/c8b3f1b470e343e7408ab5883f046b1056d94ccc/lib/time.rb" rel="nofollow">https://github.com/ruby/ruby/blob/c8b3f1b470e343e7408ab5883f046b1056d94ccc/lib/time.rb</a></p>
<p>Ruby <code>/lib/time.rb
如何在没有线程错误的情况下工作?!
</code>、<code>
等是保存正则表达式匹配结果的特殊全局变量,在内部是一个thread-local variable,因此,代码使用它们是线程安全的.
这里摘自 Ruby Documentation,
Special global variables
Pattern matching sets some global variables :
- $~ is equivalent to ::last_match;
- $& contains the complete matched text;
- $` contains string before match;
- $' contains string after match;
- , and so on contain text matching first, second, etc capture group;
- $+ contains last capture group.
These global variables are thread-local and method-local variables.
示例如下:
Thread.new {
"A B C".match(/(\w)/)
p # Prints "A"
Thread.new {
"X Y Z".match(/(\w)/)
p # Prints X
}.join
p # Prints "A", shows that was not corrupted by inner thread
}.join
在查看 RSS stdlib monkey patching the Time 类 时,我发现全局变量 </code> <code>
</code> ... 在那里被大量使用。好吧,我可以不用同时制作多个 RSS 提要。</p>
<p>但我在这里发现了同样的恐怖:<br>
- /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0//time.rb<br>
这里:<br>
- <a href="https://github.com/ruby/ruby/blob/c8b3f1b470e343e7408ab5883f046b1056d94ccc/lib/time.rb" rel="nofollow">https://github.com/ruby/ruby/blob/c8b3f1b470e343e7408ab5883f046b1056d94ccc/lib/time.rb</a></p>
<p>Ruby <code>/lib/time.rb
如何在没有线程错误的情况下工作?!
</code>、<code>
等是保存正则表达式匹配结果的特殊全局变量,在内部是一个thread-local variable,因此,代码使用它们是线程安全的.
这里摘自 Ruby Documentation,
Special global variables
Pattern matching sets some global variables :
- $~ is equivalent to ::last_match;
- $& contains the complete matched text;
- $` contains string before match;
- $' contains string after match;
- , and so on contain text matching first, second, etc capture group;
- $+ contains last capture group.
These global variables are thread-local and method-local variables.
示例如下:
Thread.new {
"A B C".match(/(\w)/)
p # Prints "A"
Thread.new {
"X Y Z".match(/(\w)/)
p # Prints X
}.join
p # Prints "A", shows that was not corrupted by inner thread
}.join