Created
August 8, 2011 13:20
-
-
Save varnie/1131727 to your computer and use it in GitHub Desktop.
Thread pool taken from http://burgestrand.se/code/ruby-thread-pool/
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' #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