views:

158

answers:

2

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?

+6  A: 

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.

Jörg W Mittag
Really? Oh well, that's too bad. That means the title of this article was misleading then: http://www.infoq.com/news/2007/08/ruby-1-9-fibers
Jesse J
+1  A: 

if you want true concurrency you'll want to use threads with jruby, I believe.

rogerdpack