Skip to content

Instantly share code, notes, and snippets.

@corny
Last active January 4, 2017 17:37
Show Gist options
  • Select an option

  • Save corny/4d7650e588917ade81d3d140c3f17390 to your computer and use it in GitHub Desktop.

Select an option

Save corny/4d7650e588917ade81d3d140c3f17390 to your computer and use it in GitHub Desktop.
Ruby consumers/producers pattern with queue.close
#!/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