Created
May 30, 2013 17:57
-
-
Save felixyz/5679768 to your computer and use it in GitHub Desktop.
Use Queue to keep synchronous interface while performing work in parallel.
Queue is thread-safe, and extremely easy to use!
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
require 'thread' | |
def provider() | |
threads = [] | |
queue = Queue.new | |
1.upto(10).each do |n| | |
threads << Thread.new { | |
sleep rand(0.1..2) | |
queue << n | |
} | |
end | |
left = 10 | |
while left > 0 | |
val = queue.pop | |
yield val | |
left -= 1 | |
end | |
end | |
provider { |s| puts s } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment