views:

602

answers:

2

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.

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

+2  A: 

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 DB queries without blocking up all the other Ruby threads.

Sam Saffron
+1  A: 

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.

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 extension 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!

Ciao!

The Doctor What