Skip to content

Instantly share code, notes, and snippets.

@felixyz
Created May 30, 2013 17:57
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 felixyz/5679768 to your computer and use it in GitHub Desktop.
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!
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