Class: Blender::Utils::ThreadPool

Inherits:
Object
  • Object
show all
Defined in:
lib/blender/utils/thread_pool.rb

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ ThreadPool

Returns a new instance of ThreadPool.



23
24
25
26
# File 'lib/blender/utils/thread_pool.rb', line 23

def initialize(size)
  @size = size
  @queue = Queue.new
end

Instance Method Details

#add_job(&blk) ⇒ Object



28
29
30
# File 'lib/blender/utils/thread_pool.rb', line 28

def add_job(&blk)
  @queue << blk
end

#run_till_doneObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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