From: Walton Hoops Date: 2010-09-15T13:17:35+09:00 Subject: Re: Ruby Multi-threading? On 9/14/2010 9:59 PM, Terry Michaels wrote: > I read a Ruby e-book recently that indicated that although Ruby has > Multi-threading capabilities, it is a fake multi-threading. I.e., the > interpreter simply switches between the executing threads, rather than > running them concurrently. > > Then recently I came across an online Ruby tutorial that specifically > said that Ruby threads could be executed in parallel on a multi-core > machine. So I'm a little confused. > > Does Ruby have real multi-threading, or is it just context switching? > This is a rather important issue for me. This is a rather confusing issue, but I'll try to give you a basic summary. MRI 1.8.7: in 1.8.7 green threads are used. This means that the interpreter is actually managing the context switching, and all "threads" are running on a single core. This does still mean that one thread may be waiting on an IO operation, such as reading the disk, while another thread is running. MRI 1.9.2: uses native (true) threads. I.E., the different threads MAY be on different CPU cores. That said, with some exceptions, code is still not executed in parallel due to the GIL (Global Interpreter Lock). The basic reason for this is to ease c-extension developers into truly concurrent threads, which as I understand it is in the starts for Ruby 2.0. JRuby: True multi-threading. In JRuby, Ruby threads are Java threads, and have all the same features and caveats. Having covered that, I strongly suggest you look into tools for concurrency via multiple processes, such as Drb (already built into the language). Shared memory threads tend to be dangerous beasts, and really aren't needed that often, especially in environments where concurrency performance is an issue.