class PatchFinder::ThreadPool

Attributes

mutex[RW]

Public Class Methods

new(size) click to toggle source

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

Public Instance Methods

cleanup() click to toggle source

Terminates all threads

@return [void]

# File lib/patch_finder/core/thread_pool.rb, line 56
def cleanup
  @jobs.clear
  @pool.map(&:kill)
end
eop?() click to toggle source

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
schedule(*args, &block) click to toggle source

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
shutdown() click to toggle source

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