Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

So, Ruby 1.9.1 is now declared stable. Rails is supposed to work with it and slowly gems are being ported to it.

It has native threads and a global interpreter lock (GIL).

Since a GIL is in place, do native threads offer any kind of benefit over green threads in 1.9.1?

like image 780
Sam Saffron Avatar asked Jul 29 '09 23:07

Sam Saffron


2 Answers

The 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.

Pros:

  • IO is now async in threads. If a thread blocks on IO, then another thread will continue until the IO is finished.
  • C extensions can use true threading.

Cons:

  • Any C extensions that aren't thread safe can have problems that are hard to find when using Thread. There is no way to mark an extension as thread unsafe to prevent it from being used with Threads.
  • The class name is the same. The native threading class should have been named something else.
  • And worst of all, threading works different on different platforms! For example, priority() is different between Solaris, Windows, and Linux. Things like loop {} run fine in Linux, other threads get a chance to run. However, on Solaris threads that thread hogs the process's time and you'll never exit!
like image 154
docwhat Avatar answered Sep 22 '22 17:09

docwhat


I think the big win, is that C based extensions can release the GIL. So, for example, a MySQL adapter can quite cleanly run long running database queries without blocking up all the other Ruby threads.

like image 37
Sam Saffron Avatar answered Sep 26 '22 17:09

Sam Saffron