I know about the "cooperative" threading of ruby using green threads. How can I create real "OS-level" threads in my application in order to make use of multiple cpu cores for processing?
相关问题
- How to let a thread communicate with another activ
- How to specify memcache server to Rack::Session::M
- Why am I getting a “C compiler cannot create execu
- reference to a method?
- Why it isn't advised to call the release() met
相关文章
- Ruby using wrong version of openssl
- Difference between Thread#run and Thread#wakeup?
- how to call a active record named scope with a str
- Java/Spring MVC: provide request context to child
- “No explicit conversion of Symbol into String” for
- Threading in C# , value types and reference types
- Segmentation fault with ruby 2.0.0p247 leading to
- RMI Threads prevent JVM from exiting after main()
It depends on the implementation:
Ruby has closures as
Blocks
,lambdas
andProcs
. To take full advantage of closures and multiple cores in JRuby, Java's executors come in handy; for MacRuby I like GCD's queues.Note that, being able to create real "OS-level" threads doesn't imply that you can use multiple cpu cores for parallel processing. Look at the examples below.
This is the output of a simple Ruby program which uses 3 threads using Ruby 2.1.0:
As you can see here, there are four OS threads, however only the one with state
R
is running. This is due to a limitation in how Ruby's threads are implemented.Same program, now with JRuby. You can see three threads with state
R
, which means they are running in parallel.The same program, now with MacRuby. There are also three threads running in parallel. This is because MacRuby threads are POSIX threads (real "OS-level" threads) and there is no GVL
Once again, the same program but now with the good old RMI. Due to the fact that this implementation uses green-threads, only one thread shows up
If you are interested in Ruby multi-threading you might find my report Debugging parallel programs using fork handlers interesting.
For a more general overview of the Ruby internals Ruby Under a Microscope is a good read.
Also, Ruby Threads and the Global Interpreter Lock in C in Omniref explains in the source code why Ruby threads don't run in parallel.
If you really need parallelism in Ruby for a Production level system (where you cannot employ a beta) processes are probably a better alternative.
But, it is most definitely worth trying threads under JRuby first.
Also if you are interested in future of threading under Ruby, you might find this article useful.
Here is some info on Rinda which is Ruby implementation of Linda (parallel processing and distributed computing paradigm) http://charmalloc.blogspot.com/2009/12/linda-tuples-rinda-drb-parallel.html