Method: Zold::ThreadPool#kill

Defined in:
lib/zold/thread_pool.rb

#killObject

Kill them all immediately and close the pool



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/zold/thread_pool.rb', line 93

def kill
  if @threads.empty?
    @log.debug("Thread pool \"#{@title}\" terminated with no threads")
    return
  end
  @log.debug("Stopping \"#{@title}\" thread pool with #{@threads.count} threads: \
#{@threads.map { |t| "#{t.name}/#{t.status}" }.join(', ')}...")
  start = Time.new
  begin
    @threads.each do |t|
      t.join(0.1)
    end
  ensure
    @threads.each do |t|
      (t.thread_variable_get(:kids) || []).each(&:kill)
      t.kill
      sleep(0.001) while t.alive? # I believe it's a bug in Ruby, this line fixes it
      Thread.current.thread_variable_set(
        :kids,
        (Thread.current.thread_variable_get(:kids) || []) - [t]
      )
    end
    @log.debug("Thread pool \"#{@title}\" terminated all threads in #{Age.new(start)}, \
it was alive for #{Age.new(@start)}: #{@threads.map { |t| "#{t.name}/#{t.status}" }.join(', ')}")
    @threads.clear
  end
end