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.



5
6
7
8
9
# File 'lib/kudzu/thread_pool.rb', line 5

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

Instance Method Details

#shutdownObject



22
23
24
25
# File 'lib/kudzu/thread_pool.rb', line 22

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

#start(&block) ⇒ Object



11
12
13
# File 'lib/kudzu/thread_pool.rb', line 11

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

#waitObject



15
16
17
18
19
20
# File 'lib/kudzu/thread_pool.rb', line 15

def wait
  until @queue.num_waiting == @threads.select { |t| t.alive? }.size
    Thread.pass
    sleep 1
  end
end