Skip to content

Instantly share code, notes, and snippets.

@cabb
Forked from ik5/thread_pool.rb
Created October 6, 2021 15:25
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 cabb/432f0c24170fc179af9b01ab7a6eea55 to your computer and use it in GitHub Desktop.
Save cabb/432f0c24170fc179af9b01ab7a6eea55 to your computer and use it in GitHub Desktop.
example on how to do threadpool in ruby
require 'thread'
queue = Queue.new
threads = []
1.step(1000) do |i|
queue << i
end
8.times do
threads << Thread.new do
until queue.empty?
work_unit = queue.pop(true) rescue nil
if work_unit
puts work_unit
sleep 5
end
end
end
end
threads.each { |t| t.join }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment