class Kudzu::ThreadPool

Public Class Methods

new(size) click to toggle source
# File lib/kudzu/thread_pool.rb, line 3
def initialize(size)
  @size = size
  @queue = Queue.new
  @threads = []
end

Public Instance Methods

shutdown() click to toggle source
# File lib/kudzu/thread_pool.rb, line 20
def shutdown
  @threads.each { |t| t.kill }
  @threads = []
end
start(&block) click to toggle source
# File lib/kudzu/thread_pool.rb, line 9
def start(&block)
  @threads = 1.upto(@size).map { create_thread(&block) }
end
wait() click to toggle source
# File lib/kudzu/thread_pool.rb, line 13
def wait
  until @queue.num_waiting == @threads.select { |t| t.alive? }.size
    Thread.pass
    sleep 1
  end
end

Private Instance Methods

create_thread(&block) click to toggle source
# File lib/kudzu/thread_pool.rb, line 27
def create_thread(&block)
  Thread.start do
    loop do
      ret = block.call(@queue)
      break if ret == :end
    end
  end
end