Class: Kudzu::ThreadPool

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

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ ThreadPool

Returns a new instance of ThreadPool.



3
4
5
6
7
# File 'lib/kudzu/thread_pool.rb', line 3

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

Instance Method Details

#shutdownObject



20
21
22
23
# File 'lib/kudzu/thread_pool.rb', line 20

def shutdown
  @threads.each { |t| t.kill }
  @threads = []
end

#start(&block) ⇒ Object



9
10
11
# File 'lib/kudzu/thread_pool.rb', line 9

def start(&block)
  @threads = 1.upto(@size).map { create_thread(&block) }
end

#waitObject



13
14
15
16
17
18
# 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