class Blender::Utils::ThreadPool

Public Class Methods

new(size) click to toggle source
# File lib/blender/utils/thread_pool.rb, line 23
def initialize(size)
  @size = size
  @queue = Queue.new
end

Public Instance Methods

add_job(&blk) click to toggle source
# File lib/blender/utils/thread_pool.rb, line 28
def add_job(&blk)
  @queue << blk
end
run_till_done() click to toggle source
# File lib/blender/utils/thread_pool.rb, line 32
def run_till_done
  num = @size > @queue.size ? @queue.size : @size
  threads = Array.new(num) do
    Thread.new do
      Thread.current.abort_on_exception = true
      @queue.pop.call while true
    end
  end
  until @queue.empty?
    sleep 0.2
  end
  until @queue.num_waiting == num
    sleep 0.2
  end
  threads.each do |thread|
    thread.join(0.02)
  end
  threads.map(&:kill)
end