views:

95

answers:

4

Hopefully this screenshot will explain my question:

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

Ruby threads demo CPU usage

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]
A: 

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
in fact, nbolton's link contains a good list of the vast number of interpreters and their threading models
Peter Recore
ruby 1.8.6 (2010-02-04 patchlevel 398) [i386-mingw32]
nbolton
If you're using 1.8.6 then it sounds like your question is answered in the link you have above. What happens when you switch to a version that actually supports native threads?
Peter Recore
A: 

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: http://stackoverflow.com/questions/1203565/native-threads-in-ruby-1-9-1-whats-in-it-for-me/1931943#1931943

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.

http://stackoverflow.com/questions/56087/does-ruby-have-real-multithreading/57802#57802

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

sir.jamesgreen
+1  A: 

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
+1  A: 

I think this answer is awesome.

http://stackoverflow.com/questions/56087/does-ruby-have-real-multithreading/57802#57802

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.

Zifei Tong