Last active
January 4, 2017 17:37
-
-
Save corny/4d7650e588917ade81d3d140c3f17390 to your computer and use it in GitHub Desktop.
Ruby consumers/producers pattern with queue.close
This file contains hidden or 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
| #!/usr/bin/env ruby | |
| require 'thread' | |
| queue = Queue.new | |
| # start producer | |
| Thread.new do | |
| 100.times do |i| | |
| queue << i | |
| end | |
| queue.close | |
| end | |
| # start consumers | |
| consumers = 10.times.map do | |
| Thread.new do | |
| while id = queue.pop | |
| puts id | |
| end | |
| end | |
| end | |
| # wait for finishing | |
| consumers.each(&:join) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment