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

前端 未结 2 424
暗喜
暗喜 2021-01-06 02:27

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

2条回答
  •  孤街浪徒
    2021-01-06 02:40

    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!

提交回复
热议问题