Skip to content

Instantly share code, notes, and snippets.

@samleb
Created August 21, 2010 19:38
Show Gist options
  • Select an option

  • Save samleb/542751 to your computer and use it in GitHub Desktop.

Select an option

Save samleb/542751 to your computer and use it in GitHub Desktop.
class FiberGroup
attr_reader :remaining
def self.run(&block)
new(&block).join
end
def initialize
@remaining = 0
@fiber = Fiber.current
yield self if block_given?
end
def join
Fiber.yield unless @remaining.zero?
end
def add(proc = nil, &block)
raise ArgumentError, "block or proc required." unless proc ||= block
@remaining += 1
Fiber.new {
proc.call
@remaining -= 1 # DO NOT MOVE UP ;)
begin
@fiber.resume if @remaining.zero?
rescue FiberError
end
}.resume
end
end
@samleb

samleb commented Aug 21, 2010

Copy link
Copy Markdown
Author

Example of usage: http://gist.github.com/542772

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment