Initializes the pool.
@param size [Fixnum] Max number of threads to be running at the same time. @return [void]
# File lib/patch_finder/core/thread_pool.rb, line 10 def initialize(size) @size = size @mutex = Mutex.new @jobs = Queue.new @pool = Array.new(@size) do |i| Thread.new do Thread.current[:id] = i catch(:exit) do loop do job, args = @jobs.pop job.call(*args) end end end end end
Terminates all threads
@return [void]
# File lib/patch_finder/core/thread_pool.rb, line 56 def cleanup @jobs.clear @pool.map(&:kill) end
Returns whether there's anything in the queue left.
@return [boolean]
# File lib/patch_finder/core/thread_pool.rb, line 49 def eop? @jobs.empty? end
Adds a job to the queue.
@param args [Array] Arguments. @param block [Proc] Code.
# File lib/patch_finder/core/thread_pool.rb, line 31 def schedule(*args, &block) @jobs << [block, args] end
Shuts down all the jobs.
@return [void]
# File lib/patch_finder/core/thread_pool.rb, line 38 def shutdown @size.times do schedule { throw :exit } end @pool.map(&:join) end