问题描述
我试图在我的程序中得到一些加速,我被告知,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 Fiber
s. Fiber
s simply aren't a concurrency construct, they are a control-flow construct, like Exception
s. That's the whole point of Fiber
s: they never run in parallel, they are cooperative and they are deterministic. Fiber
s are coroutines. (In fact, I never understood why they aren't simply called Coroutine
s.)
The only concurrency construct in Ruby is Thread
.
这篇关于Ruby纤维可以并发吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!