<p>I know about the "cooperative" threading of ruby using <a href="http://en.wikipedia.org/wiki/Green_threads" rel="noreferrer">green threads</a>. How can I create real "OS-level" threads in my application in order to make use of multiple cpu cores for processing?</p>

[{'answer_id': 56094, 'author': 'Josh Moore', 'author_id': 5004, 'author_profile': 'https://Stackoverflow.com/users/5004', 'pm_score': 5, 'selected': False, 'text': '<p>Ruby 1.8 only has green threads, there is no way to create a real "OS-level" thread.  But, ruby 1.9 will have a new feature called fibers, which will allow you to create actual OS-level threads.  Unfortunately, Ruby 1.9 is still in beta, it is scheduled to be stable in a couple of months.</p>\n\n<p>Another alternative is to use JRuby.  JRuby implements threads as OS-level theads, there are no "green threads" in it.  The latest version of JRuby is 1.1.4 and is equivalent to Ruby 1.8</p>\n'}, {'answer_id': 56361, 'author': 'ujh', 'author_id': 4936, 'author_profile': 'https://Stackoverflow.com/users/4936', 'pm_score': 2, 'selected': False, 'text': '<p>How about using <a href="http://chadfowler.com/ruby/drb.html" rel="nofollow noreferrer">drb</a>? It\'s not real multi-threading but communication between several processes, but you can use it now in 1.8 and it\'s fairly low friction.</p>\n'}, {'answer_id': 57328, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>If you are using MRI, then you can write the threaded code in C either as an extension or using the ruby-inline gem. </p>\n'}, {'answer_id': 57368, 'author': 'Pascal', 'author_id': 1311, 'author_profile': 'https://Stackoverflow.com/users/1311', 'pm_score': 1, 'selected': False, 'text': '<p>If you really need parallelism in Ruby for a Production level system (where you cannot employ a beta) processes are probably a better alternative. <br>\nBut, it is most definitely worth trying threads under JRuby first.</p>\n\n<p>Also if you are interested in future of threading under Ruby, you might find this <a href="http://www.infoq.com/news/2007/05/ruby-threading-futures" rel="nofollow noreferrer">article</a> useful.</p>\n'}, {'answer_id': 57802, 'author': 'Jörg W Mittag', 'author_id': 2988, 'author_profile': 'https://Stackoverflow.com/users/2988', 'pm_score': 10, 'selected': True, 'text': '<p><strong>Updated with Jörg\'s Sept 2011 comment</strong></p>\n\n<p>You seem to be confusing two <em>very</em> different things here: the \nRuby Programming Language and the specific threading model of one \nspecific implementation of the Ruby Programming Language. There \nare currently around 11 different implementations of the Ruby \nProgramming Language, with <em>very</em> different and unique threading \nmodels.</p>\n\n<p>(Unfortunately, only two of those 11 implementations are actually \nready for production use, but by the end of the year that number \nwill probably go up to four or five.) (<strong>Update</strong>: it\'s now 5: MRI, JRuby, YARV (the interpreter for Ruby 1.9), Rubinius and IronRuby).</p>\n\n<ol>\n<li><p>The first implementation doesn\'t actually have a name, which \nmakes it quite awkward to refer to it and is really annoying and \nconfusing. It is most often referred to as "Ruby", which is even \nmore annoying and confusing than having no name, because it \nleads to endless confusion between the features of the Ruby \nProgramming Language and a particular Ruby Implementation.</p>\n\n<p>It is also sometimes called "MRI" (for "Matz\'s Ruby \nImplementation"), CRuby or MatzRuby.</p>\n\n<p><a href="http://RedMine.Ruby-Lang.Org/repositories/annotate/ruby-18/ext/thread/thread.c" rel="noreferrer">MRI implements Ruby Threads as Green Threads within its \ninterpreter</a>. Unfortunately, it doesn\'t allow those threads \nto be scheduled in parallel, they can only run one thread at a \ntime.</p>\n\n<p>However, any number of C Threads (POSIX Threads etc.) can run \nin parallel to the Ruby Thread, so external C Libraries, or MRI \nC Extensions that create threads of their own can still run in \nparallel.</p></li>\n<li><p>The second implementation is <a href="http://WWW.AtDot.Net/yarv/" rel="noreferrer">YARV</a> (short for "Yet \nAnother Ruby VM"). <a href="http://RedMine.Ruby-Lang.Org/repositories/annotate/ruby-19/thread.c" rel="noreferrer">YARV implements Ruby Threads as POSIX or \nWindows NT Threads</a>, however, it uses a Global Interpreter \nLock (GIL) to ensure that only one Ruby Thread can actually be \nscheduled at any one time.</p>\n\n<p>Like MRI, C Threads <em>can</em> actually run parallel to Ruby Threads.</p>\n\n<p>In the future, it is possible, that the GIL <em>might</em> get broken \ndown into more fine-grained locks, thus allowing more and more \ncode to actually run in parallel, but that\'s so far away, it is \nnot even <em>planned</em> yet.</p></li>\n<li><p><a href="http://JRuby.Org/" rel="noreferrer">JRuby</a> <a href="http://www.restlessprogrammer.com/2013/02/multi-threading-in-jruby.html" rel="noreferrer">implements Ruby Threads as Native Threads</a>, \nwhere "Native Threads" in case of the JVM obviously means "JVM \nThreads". JRuby imposes no additional locking on them. So, \nwhether those threads can actually run in parallel depends on \nthe JVM: some JVMs implement JVM Threads as OS Threads and some \nas Green Threads. (The mainstream JVMs from Sun/Oracle use exclusively OS threads since JDK 1.3)</p></li>\n<li><p><a href="http://XRuby.GoogleCode.Com/" rel="noreferrer">XRuby</a> also <a href="http://Code.Google.Com/p/xruby/source/browse/trunk/src/com/xruby/runtime/builtin/RubyThread.java#21" rel="noreferrer">implements Ruby Threads as JVM Threads</a>. <strong>Update</strong>: XRuby is dead.</p></li>\n<li><p><a href="http://IronRuby.Net/" rel="noreferrer">IronRuby</a> <a href="http://IronRuby.RubyForge.Org/svn/trunk/src/IronRuby.Libraries/Builtins/ThreadOps.cs" rel="noreferrer">implements Ruby Threads as Native Threads</a>, \nwhere "Native Threads" in case of the CLR obviously means \n"CLR Threads". IronRuby imposes no additional locking on them, \nso, they should run in parallel, as long as your CLR supports \nthat.</p></li>\n<li><p><a href="http://RubyDotNetCompiler.GoogleCode.Com/" rel="noreferrer">Ruby.NET</a> also <a href="http://Code.Google.Com/p/rubydotnetcompiler/source/browse/trunk/src/RubyRuntime/Built-in%20Classes/Thread.cs#21" rel="noreferrer">implements Ruby Threads as CLR \nThreads</a>. <strong>Update:</strong> Ruby.NET is dead.</p></li>\n<li><p><a href="http://Rubini.us/" rel="noreferrer">Rubinius</a> <a href="https://GitHub.Com/EvanPhx/Rubinius/tree/master/kernel/core/thread.rb#L9" rel="noreferrer">implements Ruby Threads as Green Threads \nwithin its Virtual Machine</a>. More precisely: the Rubinius \nVM exports a very lightweight, very flexible \nconcurrency/parallelism/non-local control-flow construct, called \na "<a href="https://GitHub.Com/EvanPhx/Rubinius/tree/cpp/vm/builtin/task.cpp" rel="noreferrer">Task</a>", and all other concurrency constructs (Threads in \nthis discussion, but also <a href="https://GitHub.Com/EvanPhx/Rubinius/tree/master/kernel/core/continuation.rb#L45" rel="noreferrer">Continuations</a>, <a href="https://GitHub.Com/EvanPhx/Rubinius/tree/master/lib/actor.rb#L31" rel="noreferrer">Actors</a> and \nother stuff) are implemented in pure Ruby, using Tasks.</p>\n\n<p>Rubinius can not (currently) schedule Threads in parallel, \nhowever, adding that isn\'t too much of a problem: Rubinius can \nalready <a href="https://GitHub.Com/EvanPhx/Rubinius/tree/master/kernel/core/vm.rb#L5" rel="noreferrer">run several VM instances in several POSIX Threads in \nparallel</a>, within one Rubinius process. Since Threads are \nactually implemented in Ruby, they can, like any other Ruby \nobject, be serialized and sent to a different VM in a different \nPOSIX Thread. (That\'s the same model the BEAM <a href="http://Erlang.Org/" rel="noreferrer">Erlang</a> VM \nuses for SMP concurrency. It is already <a href="https://GitHub.Com/EvanPhx/Rubinius/tree/master/lib/vmactor.rb" rel="noreferrer">implemented for \nRubinius Actors</a>.)</p>\n\n<p><strong>Update</strong>: The information about Rubinius in this answer is about the Shotgun VM, which doesn\'t exist anymore. The "new" C++ VM does not use green threads scheduled across multiple VMs (i.e. Erlang/BEAM style), it uses a more traditional single VM with multiple native OS threads model, just like the one employed by, say, the CLR, Mono, and pretty much every JVM.</p></li>\n<li><p><a href="http://MacRuby.Org/" rel="noreferrer">MacRuby</a> started out as a port of YARV on top of the \nObjective-C Runtime and CoreFoundation and Cocoa Frameworks. It \nhas now significantly diverged from YARV, but AFAIK it currently \nstill <a href="http://MacRuby.Org/trac/browser/MacRuby/trunk/thread.c?annotate=blame" rel="noreferrer">shares the same Threading Model with YARV</a>.\n<strong>Update:</strong> MacRuby depends on apples garbage collector which is declared deprecated and will be removed in later versions of MacOSX, MacRuby is undead.</p></li>\n<li><p><a href="https://github.com/parrot/cardinal" rel="noreferrer">Cardinal</a> is a Ruby Implementation for the <a href="http://ParrotCode.Org/" rel="noreferrer">Parrot \nVirtual Machine</a>. It doesn\'t implement threads yet, however, \nwhen it does, it will probably implement them as <a href="http://SVN.Perl.Org/viewvc/parrot/trunk/src/thread.c?view=annotate" rel="noreferrer">Parrot \nThreads</a>. <strong>Update</strong>: Cardinal seems very inactive/dead.</p></li>\n<li><p><a href="http://MagLev.GemStone.Com/" rel="noreferrer">MagLev</a> is a Ruby Implementation for the <a href="http://GemStone.Com/products/smalltalk/" rel="noreferrer">GemStone/S \nSmalltalk VM</a>. I have no information what threading model \nGemStone/S uses, what threading model MagLev uses or even if \nthreads are even implemented yet (probably not).</p></li>\n<li><p><a href="http://HotRuby.Yukoba.Jp/" rel="noreferrer">HotRuby</a> is <em>not</em> a full Ruby Implementation of its \nown. It is an implementation of a YARV bytecode VM in \nJavaScript. HotRuby doesn\'t support threads (yet?) and when it \ndoes, they won\'t be able to run in parallel, because JavaScript \nhas no support for true parallelism. There is an ActionScript \nversion of HotRuby, however, and ActionScript might actually \nsupport parallelism. <strong>Update</strong>: HotRuby is dead.</p></li>\n</ol>\n\n<p>Unfortunately, only two of these 11 Ruby Implementations are \nactually production-ready: MRI and JRuby.</p>\n\n<p>So, if you want true parallel threads, JRuby is currently your \nonly choice\xa0– not that that\'s a bad one: JRuby is actually faster \nthan MRI, and arguably more stable.</p>\n\n<p>Otherwise, the "classical" Ruby solution is to use processes \ninstead of threads for parallelism. The Ruby Core Library \ncontains the <a href="http://Ruby-Doc.Org/core/classes/Process.html" rel="noreferrer"><code>Process</code> module</a> with the <a href="http://Ruby-Doc.Org/core/classes/Process.html#M003179" rel="noreferrer"><code>Process.fork</code> \nmethod</a> which makes it dead easy to fork off another Ruby \nprocess. Also, the Ruby Standard Library contains the \n<a href="http://Ruby-Doc.Org/stdlib/libdoc/drb/rdoc/" rel="noreferrer">Distributed Ruby (dRuby / dRb)</a> library, which allows Ruby \ncode to be trivially distributed across multiple processes, not \nonly on the same machine but also across the network.</p>\n'}, {'answer_id': 1987565, 'author': 'Joe Stein', 'author_id': 241770, 'author_profile': 'https://Stackoverflow.com/users/241770', 'pm_score': 1, 'selected': False, 'text': '<p>Here is some info on Rinda which is Ruby implementation of Linda (parallel processing and distributed computing paradigm) <a href="http://charmalloc.blogspot.com/2009/12/linda-tuples-rinda-drb-parallel.html" rel="nofollow noreferrer">http://charmalloc.blogspot.com/2009/12/linda-tuples-rinda-drb-parallel.html</a></p>\n'}, {'answer_id': 21531941, 'author': 'user454322', 'author_id': 454322, 'author_profile': 'https://Stackoverflow.com/users/454322', 'pm_score': 3, 'selected': False, 'text': '<p>It depends on the implementation: <br></p>\n\n<ul>\n<li>MRI doesn\'t have, YARV is closer.</li>\n<li>JRuby and MacRuby have.</li>\n</ul>\n\n<p><br></p>\n\n<p><br> <br>\nRuby has <a href="http://en.wikipedia.org/wiki/Closure_%28computer_programming%29" rel="nofollow noreferrer">closures</a> as <code>Blocks</code>, <code>lambdas</code> and <code>Procs</code>. To take full advantage of closures and multiple cores in JRuby, <a href="http://blog.bitmelt.com/2013/05/concurrent-tasks-in-jruby.html" rel="nofollow noreferrer">Java\'s executors</a> come in handy; for MacRuby I like <a href="http://seanlilmateus.github.io/blog/2012/05/31/getting-started-with-grand-central-dispatch-in-macruby-and-rubymotion/" rel="nofollow noreferrer">GCD\'s queues</a>.\n<br><br>\nNote that, being able to create <em>real "OS-level" threads</em> doesn\'t imply that you can use multiple cpu cores for parallel processing. Look at the examples below.</p>\n\n<p>This is the output of <a href="https://gist.github.com/user454322/8785580" rel="nofollow noreferrer">a simple Ruby program which uses 3 threads</a> using Ruby 2.1.0:</p>\n\n<pre><code>(jalcazar@mac ~)$ ps -M 69877\nUSER     PID   TT   %CPU STAT PRI     STIME     UTIME COMMAND\njalcazar 69877 s002    0.0 S    31T   0:00.01   0:00.04 /Users/jalcazar/.rvm/rubies/ruby-2.1.0/bin/ruby threads.rb\n   69877         0.0 S    31T   0:00.01   0:00.00 \n   69877        33.4 S    31T   0:00.01   0:08.73 \n   69877        43.1 S    31T   0:00.01   0:08.73 \n   69877        22.8 R    31T   0:00.01   0:08.65 \n</code></pre>\n\n<p>As you can see here, there are four OS threads, however only the one with state <code>R</code> is running. This is due to a limitation in how Ruby\'s threads are implemented.</p>\n\n<hr>\n\n<p><br>\nSame program, now with JRuby. You can see three threads with state <code>R</code>, which means they are running in parallel.</p>\n\n<pre><code>(jalcazar@mac ~)$ ps -M 72286\nUSER     PID   TT   %CPU STAT PRI     STIME     UTIME COMMAND\njalcazar 72286 s002    0.0 S    31T   0:00.01   0:00.01 /Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home/bin/java -Djdk.home= -Djruby.home=/Users/jalcazar/.rvm/rubies/jruby-1.7.10 -Djruby.script=jruby -Djruby.shell=/bin/sh -Djffi.boot.library.path=/Users/jalcazar/.rvm/rubies/jruby-1.7.10/lib/jni:/Users/jalcazar/.rvm/rubies/jruby-1.7.10/lib/jni/Darwin -Xss2048k -Dsun.java.command=org.jruby.Main -cp  -Xbootclasspath/a:/Users/jalcazar/.rvm/rubies/jruby-1.7.10/lib/jruby.jar -Xmx1924M -XX:PermSize=992m -Dfile.encoding=UTF-8 org/jruby/Main threads.rb\n   72286         0.0 S    31T   0:00.00   0:00.00 \n   72286         0.0 S    33T   0:00.00   0:00.00 \n   72286         0.0 S    31T   0:00.09   0:02.34 \n   72286         7.9 S    31T   0:00.15   0:04.63 \n   72286         0.0 S    31T   0:00.00   0:00.00 \n   72286         0.0 S    31T   0:00.00   0:00.00 \n   72286         0.0 S    31T   0:00.00   0:00.00 \n   72286         0.0 S    31T   0:00.04   0:01.68 \n   72286         0.0 S    31T   0:00.03   0:01.54 \n   72286         0.0 S    31T   0:00.00   0:00.00 \n   72286         0.0 S    31T   0:00.01   0:00.01 \n   72286         0.0 S    31T   0:00.00   0:00.01 \n   72286         0.0 S    31T   0:00.00   0:00.03 \n   72286        74.2 R    31T   0:09.21   0:37.73 \n   72286        72.4 R    31T   0:09.24   0:37.71 \n   72286        74.7 R    31T   0:09.24   0:37.80 \n</code></pre>\n\n<hr>\n\n<p><br>\nThe same program, now with MacRuby. There are also three threads running in parallel. This is because <a href="https://github.com/MacRuby/MacRuby/wiki/Why-MacRuby%3F#2-thread-support" rel="nofollow noreferrer">MacRuby threads are POSIX threads</a> (<em>real "OS-level" threads</em>) and there is <a href="http://vimeo.com/67731482#t=19m47s" rel="nofollow noreferrer">no GVL</a></p>\n\n<pre><code>(jalcazar@mac ~)$ ps -M 38293\nUSER     PID   TT   %CPU STAT PRI     STIME     UTIME COMMAND\njalcazar 38293 s002    0.0 R     0T   0:00.02   0:00.10 /Users/jalcazar/.rvm/rubies/macruby-0.12/usr/bin/macruby threads.rb\n   38293         0.0 S    33T   0:00.00   0:00.00 \n   38293       100.0 R    31T   0:00.04   0:21.92 \n   38293       100.0 R    31T   0:00.04   0:21.95 \n   38293       100.0 R    31T   0:00.04   0:21.99 \n</code></pre>\n\n<hr>\n\n<p><br>\nOnce again, the same program but now with the good old MRI. Due to the fact that this implementation uses green-threads, only one thread shows up</p>\n\n<pre><code>(jalcazar@mac ~)$ ps -M 70032\nUSER     PID   TT   %CPU STAT PRI     STIME     UTIME COMMAND\njalcazar 70032 s002  100.0 R    31T   0:00.08   0:26.62 /Users/jalcazar/.rvm/rubies/ruby-1.8.7-p374/bin/ruby threads.rb\n</code></pre>\n\n<p><br><br>\nIf you are interested in Ruby multi-threading you might find my report <em><a href="https://modprobe.files.wordpress.com/2015/09/jalcazar_pmam20151.pdf" rel="nofollow noreferrer">Debugging parallel programs using fork handlers</a></em> interesting.<br>\nFor a more general overview of the Ruby internals <em><a href="http://shop.oreilly.com/product/9781593275273.do" rel="nofollow noreferrer">Ruby Under a Microscope</a></em> is a good read. <br>\nAlso, <a href="https://www.omniref.com/ruby/2.2.0.preview1/files/thread_pthread.h#annotation=4057266&amp;line=38" rel="nofollow noreferrer">Ruby Threads and the Global Interpreter Lock in C</a> in Omniref explains in the source code why Ruby threads don\'t run in parallel.</p>\n'}, {'answer_id': 28664487, 'author': 'GroovyCakes', 'author_id': 736524, 'author_profile': 'https://Stackoverflow.com/users/736524', 'pm_score': 2, 'selected': False, 'text': '<p>I\'ll let the "System Monitor" answer this question.  I\'m executing the same code (below, which calculates prime numbers) with 8 Ruby threads running on an i7 (4 hyperthreaded-core) machine in both cases... the first run is with:</p>\n\n<p>jruby 1.5.6 (ruby 1.8.7 patchlevel 249) (2014-02-03 6586)\n(OpenJDK 64-Bit Server VM 1.7.0_75) [amd64-java] </p>\n\n<p>The second is with:</p>\n\n<p>ruby 2.1.2p95 (2014-05-08) [x86_64-linux-gnu]</p>\n\n<p>Interestingly, the CPU is higher for JRuby threads, but the time to completion is slightly shorter for the interpreted Ruby.  It\'s kind of difficult to tell from the graph, but the second (interpreted Ruby) run uses about 1/2 the CPUs (no hyperthreading?)</p>\n\n<p><img src="https://i.stack.imgur.com/haINx.png" alt="enter image description here"></p>\n\n<pre><code>def eratosthenes(n)\n  nums = [nil, nil, *2..n]\n  (2..Math.sqrt(n)).each do |i|\n    (i**2..n).step(i){|m| nums[m] = nil}  if nums[i]\n  end\n  nums.compact\nend\n\nMAX_PRIME=10000000\nTHREADS=8\nthreads = []\n\n1.upto(THREADS) do |num|\n  puts "Starting thread #{num}"\n  threads[num]=Thread.new { eratosthenes MAX_PRIME }\nend\n\n1.upto(THREADS) do |num|\n    threads[num].join\nend\n</code></pre>\n'}, {'answer_id': 43850237, 'author': 'zw963', 'author_id': 749774, 'author_profile': 'https://Stackoverflow.com/users/749774', 'pm_score': 1, 'selected': False, 'text': '<p>Because could not edit that answer, so add a new reply here.</p>\n\n<p><strong>Update(2017-05-08)</strong></p>\n\n<p>This article is very old, and information is not follow current\n(2017) tread, Following is some supplement:</p>\n\n<ol start="12">\n<li><p><a href="https://github.com/opal/opal" rel="nofollow noreferrer">Opal</a> is a Ruby to JavaScript source-to-source compiler. It also has an implementation of the Ruby corelib, It current very active develompent, and exist a great deal of (frontend) framework worked on it.\nand production ready. Because base on javascript, it not support parallel threads.</p></li>\n<li><p><a href="https://github.com/graalvm/truffleruby" rel="nofollow noreferrer">truffleruby</a> is a high performance implementation of the Ruby programming language. Built on the GraalVM by Oracle Labs,TruffleRuby is a fork of JRuby, combining it with code from the Rubinius project, and also containing code from the standard implementation of Ruby, MRI, still live development, not production ready.\nThis version ruby seem like born for performance, I don\'t know if support parallel threads, but I think it should.</p></li>\n</ol>\n'}]
['https://Stackoverflow.com/questions/56087', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3205/']