Ruby纤维可以并发吗? [英] Can Ruby Fibers be Concurrent?

查看:194
本文介绍了Ruby纤维可以并发吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的程序中得到一些加速,我被告知,Ruby Fibers比线程更快,可以利用多个内核。我已经环顾四周,但我只是不能找到如何实际运行不同的纤维并发。使用主题您可以这样做:

  threads = [] 

threads< Thread.new {Do something}
threads<< Thread.new {Do something}

threads.each {| thread | thread.join}

我看不到如何用纤维做这样的事情。所有我可以找到 yield resume 这似乎只是一束在纤维之间的开始和停止。有没有办法用光纤做真正的并发?

解决方案

不,你不能用 s。 Fiber 根本不是并发结构,它们是一个控制流构造,例如 Exception 。这是 Fiber 整点:他们永远不会并行运行,他们是合作的,他们是确定性的。 Fiber 是协同程序。 (实际上,我从来没有理解为什么他们不是简单地调用 Coroutine 。)



唯一的并发结构在Ruby中线程


I'm trying to get some speed up in my program and I've been told that Ruby Fibers are faster than threads and can take advantage of multiple cores. I've looked around, but I just can't find how to actually run different fibers concurrently. With threads you can do this:

threads = []

threads << Thread.new {Do something}
threads << Thread.new {Do something}

threads.each {|thread| thread.join}

I can't see how to do something like this with fibers. All I can find is yield and resume which seems like just a bunch of starting and stopping between the fibers. Is there a way to do true concurrency with fibers?

解决方案

No, you cannot do concurrency with Fibers. Fibers simply aren't a concurrency construct, they are a control-flow construct, like Exceptions. That's the whole point of Fibers: they never run in parallel, they are cooperative and they are deterministic. Fibers are coroutines. (In fact, I never understood why they aren't simply called Coroutines.)

The only concurrency construct in Ruby is Thread.

这篇关于Ruby纤维可以并发吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆