Skip to content

Instantly share code, notes, and snippets.

@nerdyworm
Created January 24, 2011 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nerdyworm/794244 to your computer and use it in GitHub Desktop.
Save nerdyworm/794244 to your computer and use it in GitHub Desktop.
User of a Fiber block to create a simple producer consumer thing.
@b = nil
@x = nil
@y = nil
@z = nil
def producer
Fiber.new do
loop do
@b = rand()
Fiber.yield #or consumer resume
@b = rand()
Fiber.yield
end
end
end
def consumer
Fiber.new do
loop do
@x = @b
producer.resume
@y = @b
producer.resume
@z = @b
producer.resume
puts "x = #{@x} y = #{@y} z = #{@z}"
end
end
end
consumer.resume
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment