Ruby 1.9.3 multicore?

北慕城南 提交于 2019-12-10 16:14:08

问题


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.

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

Is this really possible (even with more cores?), or am I just missing something, and the test is wrong?


回答1:


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

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.



来源:https://stackoverflow.com/questions/11574905/ruby-1-9-3-multicore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!