7

Hopefully this screenshot will explain my question:

a = Thread.new { loop {} }
b = Thread.new { loop {} }
a.join

Ruby threads demo CPU usage http://img7.imageshack.us/img7/9858/rubycores.png

So how come both of my cores aren't maxed out? No matter how many threads I use, it's the same each time; the total CPU usage never seems to exceed 52%.

>ruby -v
ruby 1.8.6 (2010-02-04 patchlevel 398) [i386-mingw32]
Nick Bolton
  • 38,276
  • 70
  • 174
  • 242
  • Could be related to or a duplicate of: http://stackoverflow.com/questions/56087/does-ruby-have-real-multithreading – Nick Bolton Aug 06 '10 at 19:41

4 Answers4

4

It looks like you are using MRI, which is incapable of running threads in parallel. At the moment, the only production-ready Ruby implementations which can run threads in parallel are JRuby and IronRuby.

Remember, if you want threads to actually run in parallel, then every layer in the stack must be able to do that. Take JRuby, for example: JRuby can run Ruby threads in parallel. However, it implements threads by mapping them to JVM threads, so if the JVM is incapable of running threads in parallel (and there are some for which this is the case), then the fact that JRuby can run Ruby threads in parallel doesn't help you one bit. Many JVMs, in turn, map JVM threads to OS threads. And again: if the OS isn't capable of running threads in parallel, there's nothing the JVM can do. And last but not least: if there's only one processor, the whole exercise is pointless anyway.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • hey is this still the case with MRI? trying to figure this out – Frank Visaggio Nov 13 '13 at 14:59
  • 1
    @BobSinclar: Yes. MRI does not support running multiple threads at the same time. And MRI is no longer being developed, so it never will. YARV also doesn't support it, though it might in the future. JRuby, IronRuby and Rubinius (> 2.0) *do* support it. Not sure about MacRuby and MagLev. – Jörg W Mittag Nov 13 '13 at 15:03
2

I think this answer is awesome.

Does ruby have real multithreading?

Since your are using Ruby 1.8.6, the MRI Implementation. This quotation form the URL above explain why only one core is used.

MRI implements Ruby Threads as Green Threads within its interpreter. Unfortunately, it doesn't allow those threads to be scheduled in parallel, they can only run one thread at a time.

Note that MacRuby (sort of a port of YARV) removed the GIL recently. So your demo code will use both cores with MacRuby 0.5 or later version. For now it is the only Ruby Implementation that can run in parallel that not depending on JVM or CLR.

Community
  • 1
  • 1
Zifei Tong
  • 1,697
  • 1
  • 19
  • 32
1

The main thing to remember is that there is a difference between Ruby (language) and Ruby (implementation). You don't make it clear which you mean, but as you are having problems I assume you mean Ruby (implementation).

Looking at some previous answers :

Ruby 1.9.1: Native threads in Ruby 1.9.1, whats in it for me?

The ruby threads in 1.9 are native, but they have been "slowed down" to allow only one thread to run at a time. This is because it would confuse existing code, if the threads really ran in parallel.

Does ruby have real multithreading?

This gives a fantastic explanation of Ruby Threading on various Ruby VMs.

Community
  • 1
  • 1
James Greenhalgh
  • 2,401
  • 18
  • 17
0

What version of ruby are you using, and which interpreter? (JRuby, "regular ruby", etc?)

Not all ruby interpreters can take advantage of multiple cores/processors.

Peter Recore
  • 14,037
  • 4
  • 42
  • 62