Ruby 1.9.3多核吗? [英] Ruby 1.9.3 multicore?

查看:95
本文介绍了Ruby 1.9.3多核吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Yestereday,我读了一些有关红宝石穿线的信息(例如本文),而我通常理解的是(除了一些类似JRuby的实现),存在所谓的Global Interpreter Lock,因此,一个人不能一次在多个CPU上运行ruby代码. 我做了一点测试(我有AMD Turion II双核Mobile M500处理器,并运行ubuntu 11.04,+ rvm),并通过以下代码看到了实际效果:

Yestereday I read a little about threading in ruby (like this article), and what I generally understood was, that (except a few implementations like JRuby), there is the so-called Global Interpreter Lock, and because of that, one cannot run ruby code on multiple CPUs at a time. I did a little test (I have AMD Turion II Dual-Core Mobile M500 processors, and running ubuntu 11.04, +rvm), to see this in action, with this code:

threads = []
CPU = 2

CPU.times do
  threads << Thread.new {
    x=0
    time=Time.new
    while 1 do
      if Time.new - time >= 30 then
        break
      else
        x=1.00/24000000000.001
      end
    end
  }
  end
threads.each { |t| t.join }
puts "done"

并获取了系统监视器的屏幕截图.

And took screenshots of the system monitor.

对我来说,REE和1.9.2似乎一次使用一个内核,但是1.9.3似乎同时使用了两个内核.

For me it seems, that REE and 1.9.2 uses one core at a time, but 1.9.3 seems to utilize both.

这是否真的可能(甚至有更多的内核?),或者我只是缺少某些东西,而测试是错误的?

推荐答案

存在所谓的全局解释器锁,因此,一个锁不能一次在多个CPU上运行ruby代码

there is the so-called Global Interpreter Lock, and because of that, one cannot run ruby code on multiple CPUs at a time

这只是部分正确.扩展代码(用C编写)通常不支持并行执行(不是线程安全的).因此,对于扩展调用,ruby解释器使用GIL.但是您的代码不会调用任何扩展名(我不确定,可能只是Time.new需要锁定).因此,Ruby在许多情况下(以及您的代码示例)可以利用多个CPU .

It's only partially true. Code of extensions (written in C) often not support parallel execution (not thread safe). And so for extensions calls ruby interpreter uses GIL. But your code doesn't call any extensions (only may be Time.new require lock, I'm not sure). And so ruby in many cases (and your code example) can utilize multiple CPU.

这篇关于Ruby 1.9.3多核吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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