tpv
tpv

Reputation: 153

Ruby 1.9.3 multicore?

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?

Upvotes: 4

Views: 1613

Answers (1)

valodzka
valodzka

Reputation: 5805

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.

Upvotes: 3

Related Questions