Created
January 24, 2011 23:59
-
-
Save nerdyworm/794244 to your computer and use it in GitHub Desktop.
User of a Fiber block to create a simple producer consumer thing.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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