我知道红宝石的“合作”线程使用绿色线程。如何在我的应用程序中创建真正的“操作系统级”线程,以便使用多个cpu内核进行处理?
当前回答
如果您正在使用MRI,那么您可以用C编写线程代码作为扩展或使用ruby-inline gem。
其他回答
Ruby 1.8只有绿色线程,没有办法创建一个真正的“操作系统级”线程。但是,ruby 1.9将有一个叫做光纤的新特性,它将允许您创建实际的操作系统级线程。不幸的是,Ruby 1.9仍处于测试阶段,预计几个月后就会稳定下来。
另一种选择是使用JRuby。JRuby将线程实现为操作系统级别的头,其中没有“绿色线程”。JRuby的最新版本是1.1.4,相当于Ruby 1.8
使用drb怎么样?它不是真正的多线程,而是几个进程之间的通信,但你现在可以在1.8中使用它,而且它的阻力相当低。
如果您正在使用MRI,那么您可以用C编写线程代码作为扩展或使用ruby-inline gem。
如果您确实需要Ruby在产品级系统中的并行性(您不能使用beta),那么进程可能是更好的选择。 但是,首先在JRuby下尝试线程绝对是值得的。
另外,如果您对Ruby下线程的未来感兴趣,您可能会发现本文很有用。
更新自Jörg 2011年9月的评论
你好像搞混了两个完全不同的东西 Ruby编程语言和特定的线程模型之一 Ruby编程语言的具体实现。在那里 目前大约有11种不同的Ruby实现 编程语言,具有非常不同和独特的线程 模型。
(不幸的是,这11个实现中只有两个是真正的 准备投入生产使用,但要到年底这个数字 (更新:现在是5个:MRI, JRuby, YARV (Ruby 1.9的解释器),Rubinius和IronRuby)。
The first implementation doesn't actually have a name, which makes it quite awkward to refer to it and is really annoying and confusing. It is most often referred to as "Ruby", which is even more annoying and confusing than having no name, because it leads to endless confusion between the features of the Ruby Programming Language and a particular Ruby Implementation. It is also sometimes called "MRI" (for "Matz's Ruby Implementation"), CRuby or MatzRuby. MRI implements Ruby Threads as Green Threads within its interpreter. Unfortunately, it doesn't allow those threads to be scheduled in parallel, they can only run one thread at a time. However, any number of C Threads (POSIX Threads etc.) can run in parallel to the Ruby Thread, so external C Libraries, or MRI C Extensions that create threads of their own can still run in parallel. The second implementation is YARV (short for "Yet Another Ruby VM"). YARV implements Ruby Threads as POSIX or Windows NT Threads, however, it uses a Global Interpreter Lock (GIL) to ensure that only one Ruby Thread can actually be scheduled at any one time. Like MRI, C Threads can actually run parallel to Ruby Threads. In the future, it is possible, that the GIL might get broken down into more fine-grained locks, thus allowing more and more code to actually run in parallel, but that's so far away, it is not even planned yet. JRuby implements Ruby Threads as Native Threads, where "Native Threads" in case of the JVM obviously means "JVM Threads". JRuby imposes no additional locking on them. So, whether those threads can actually run in parallel depends on the JVM: some JVMs implement JVM Threads as OS Threads and some as Green Threads. (The mainstream JVMs from Sun/Oracle use exclusively OS threads since JDK 1.3) XRuby also implements Ruby Threads as JVM Threads. Update: XRuby is dead. IronRuby implements Ruby Threads as Native Threads, where "Native Threads" in case of the CLR obviously means "CLR Threads". IronRuby imposes no additional locking on them, so, they should run in parallel, as long as your CLR supports that. Ruby.NET also implements Ruby Threads as CLR Threads. Update: Ruby.NET is dead. Rubinius implements Ruby Threads as Green Threads within its Virtual Machine. More precisely: the Rubinius VM exports a very lightweight, very flexible concurrency/parallelism/non-local control-flow construct, called a "Task", and all other concurrency constructs (Threads in this discussion, but also Continuations, Actors and other stuff) are implemented in pure Ruby, using Tasks. Rubinius can not (currently) schedule Threads in parallel, however, adding that isn't too much of a problem: Rubinius can already run several VM instances in several POSIX Threads in parallel, within one Rubinius process. Since Threads are actually implemented in Ruby, they can, like any other Ruby object, be serialized and sent to a different VM in a different POSIX Thread. (That's the same model the BEAM Erlang VM uses for SMP concurrency. It is already implemented for Rubinius Actors.) Update: The information about Rubinius in this answer is about the Shotgun VM, which doesn't exist anymore. The "new" C++ VM does not use green threads scheduled across multiple VMs (i.e. Erlang/BEAM style), it uses a more traditional single VM with multiple native OS threads model, just like the one employed by, say, the CLR, Mono, and pretty much every JVM. MacRuby started out as a port of YARV on top of the Objective-C Runtime and CoreFoundation and Cocoa Frameworks. It has now significantly diverged from YARV, but AFAIK it currently still shares the same Threading Model with YARV. Update: MacRuby depends on apples garbage collector which is declared deprecated and will be removed in later versions of MacOSX, MacRuby is undead. 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. Update: Cardinal seems very inactive/dead. 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). 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. Update: HotRuby is dead.
不幸的是,这11个Ruby实现中只有两个是正确的 实际生产就绪:MRI和JRuby。
因此,如果您想要真正的并行线程,JRuby目前是您的首选 唯一的选择——并不是说这是一个坏的选择:JRuby实际上更快 比核磁共振成像更稳定。
否则,“经典的”Ruby解决方案就是使用进程 而不是并行的线程。Ruby核心库 包含Process模块和Process.fork 方法,这使得分叉另一个Ruby非常容易 的过程。此外,Ruby标准库包含 分布式Ruby (dRuby / dRb)库,它允许Ruby 代码应该简单地分布在多个进程中,而不是 只能在同一台机器上,但也可以跨网络。
推荐文章
- 如何找到包含匹配值的哈希键
- ExecutorService,如何等待所有任务完成
- 在python中创建线程
- 如何在Rails中找到当前的路由?
- 在Ruby中->运算符叫什么?
- Rails参数解释?
- Ruby中DateTime和Time的区别
- 如何从代理服务器后面更新Ruby Gems (ISA-NTLM)
- 如何用另一个键替换哈希键
- attr_accessor和attr_accessible的区别
- 处理来自Java ExecutorService任务的异常
- 如何从Ruby文件路径中获得没有扩展名的文件名
- rvm安装失败:“rvm不是一个函数”
- 学习Ruby on Rails
- Ruby中的数组切片:解释不合逻辑的行为(摘自Rubykoans.com)