Ruby对多线程的支持
程序员文章站
2022-05-25 16:05:56
...
1. 我们通常说的Ruby,一般指的MRI 即"Matz's Ruby Implementation", CRuby or MatzRuby,MRI的在解释器里的Ruby线程是Green Thread,而且不幸的,这些线程不予能并行地调度,因此一次只能跑一个线程。
不过扩展的C类库(比如Fiber),可以并行地运行多个C线程。
2. 接下来是YARV(即Yet Another Ruby VM),YARV的线程是以POSIX或者WinNT线程实现的,它采用了Global Interpreter Lock (GIL)来确保在任何时候只有一个Ruby线程可以被调度。
和MRI一样,C线程可以和Ruby线程同时运行。未来, GIL可能会被fine-grained locks取代,这样越来越多的代码就可以并行地运行,不过这个还没提上议事日程呢。
3. JRuby以原生线程的方式实现Ruby线程,这儿的“原生线程”当然指的是"JVM线程“。JRuby并未增加额外的锁。因此,这些线程是否能并行的运行,取决于JVM,有些JVM采用了OS线程而有些只是Green Treahd.
4. XRuby也是以JVM线程的方式实现Ruby线程。
5. IronRuby也是以原生线程的方式实现ruby线程,这儿的“原生线程”指的是CLR线程,Ironruby也未增加额外的锁,因此只要你的CLR支持多线程,你的Ironruby线程就能并行
运行。
6. Ruby.NET也是以CLR线程的方式实现Ruby线程。
7. Rubinius在它的虚拟机中以Green Thread的方式实现Ruby线程。特别之处的是Rubinius的虚拟机十分轻巧,具有非常弹性的concurrency/parallelism/non-local的结构,称为“任务”,而其他的concurrency constructs (Threads in this discussion, but also Continuations, Actors and other stuff) are implemented in pure Ruby, using Tasks.
Rubinius 现在无法并行地调度线程,虽然正佳这项特性并无太大问题。 Rubinius可以在一个进程的多个POSIX线程中运行多个虚拟机实例。由于线程是采用Ruby语言实现的,因此它们可以像其他Ruby对象一样,被串行化到另一个线程中的另一个虚拟机实例。 (That's the same model the BEAM Erlang VM uses for SMP concurrency. It is already implemented for Rubinius Actors.)
8. MacRuby开始只是YARV在Objective-C运行时和CoreFoundation Cocoa Frameworks上的实现. It has now significantly diverged from YARV, but AFAIK it currently still shares the same Threading Model with YARV.
9.
Cardinal is a Ruby Implementation for the Parrot Virtual Machine. It doesn't implement threads yet, however, when it does, it will probably implement them as Parrot Threads.
10.
MagLev is a Ruby Implementation for the GemStone/S Smalltalk VM. I have no information what threading model GemStone/S uses, what threading model MagLev uses or even if threads are even implemented yet (probably not).
11.
HotRuby is not a full Ruby Implementation of its own. It is an implementation of a YARV bytecode VM in JavaScript. HotRuby doesn't support threads (yet?) and when it does, they won't be able to run in parallel, because JavaScript has no support for true parallelism. There is an ActionScript version of HotRuby, however, and ActionScript might actually support parallelism.
不过扩展的C类库(比如Fiber),可以并行地运行多个C线程。
2. 接下来是YARV(即Yet Another Ruby VM),YARV的线程是以POSIX或者WinNT线程实现的,它采用了Global Interpreter Lock (GIL)来确保在任何时候只有一个Ruby线程可以被调度。
和MRI一样,C线程可以和Ruby线程同时运行。未来, GIL可能会被fine-grained locks取代,这样越来越多的代码就可以并行地运行,不过这个还没提上议事日程呢。
3. JRuby以原生线程的方式实现Ruby线程,这儿的“原生线程”当然指的是"JVM线程“。JRuby并未增加额外的锁。因此,这些线程是否能并行的运行,取决于JVM,有些JVM采用了OS线程而有些只是Green Treahd.
4. XRuby也是以JVM线程的方式实现Ruby线程。
5. IronRuby也是以原生线程的方式实现ruby线程,这儿的“原生线程”指的是CLR线程,Ironruby也未增加额外的锁,因此只要你的CLR支持多线程,你的Ironruby线程就能并行
运行。
6. Ruby.NET也是以CLR线程的方式实现Ruby线程。
7. Rubinius在它的虚拟机中以Green Thread的方式实现Ruby线程。特别之处的是Rubinius的虚拟机十分轻巧,具有非常弹性的concurrency/parallelism/non-local的结构,称为“任务”,而其他的concurrency constructs (Threads in this discussion, but also Continuations, Actors and other stuff) are implemented in pure Ruby, using Tasks.
Rubinius 现在无法并行地调度线程,虽然正佳这项特性并无太大问题。 Rubinius可以在一个进程的多个POSIX线程中运行多个虚拟机实例。由于线程是采用Ruby语言实现的,因此它们可以像其他Ruby对象一样,被串行化到另一个线程中的另一个虚拟机实例。 (That's the same model the BEAM Erlang VM uses for SMP concurrency. It is already implemented for Rubinius Actors.)
8. MacRuby开始只是YARV在Objective-C运行时和CoreFoundation Cocoa Frameworks上的实现. It has now significantly diverged from YARV, but AFAIK it currently still shares the same Threading Model with YARV.
9.
Cardinal is a Ruby Implementation for the Parrot Virtual Machine. It doesn't implement threads yet, however, when it does, it will probably implement them as Parrot Threads.
10.
MagLev is a Ruby Implementation for the GemStone/S Smalltalk VM. I have no information what threading model GemStone/S uses, what threading model MagLev uses or even if threads are even implemented yet (probably not).
11.
HotRuby is not a full Ruby Implementation of its own. It is an implementation of a YARV bytecode VM in JavaScript. HotRuby doesn't support threads (yet?) and when it does, they won't be able to run in parallel, because JavaScript has no support for true parallelism. There is an ActionScript version of HotRuby, however, and ActionScript might actually support parallelism.
推荐阅读