Skip to content

Instantly share code, notes, and snippets.

@varnie
Created August 8, 2011 13:20
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 varnie/1131727 to your computer and use it in GitHub Desktop.
Save varnie/1131727 to your computer and use it in GitHub Desktop.
require 'thread' #taken from http://burgestrand.se/code/ruby-thread-pool/
class Pool
def initialize(size)
@size=size
@jobs = Queue.new
@pool = Array.new(@size) do |i|
Thread.new do
Thread.current[:id] = i
catch(:exit) do
loop do
job, args = @jobs.pop
job.call(*args)
end
end
end
end
end
def schedule(*args, &block)
@jobs << [block, args]
end
def shutdown
@size.times do
schedule {throw :exit }
end
@pool.map(&:join)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment