Sha256: 214dabfca4edb1f5ec0f36ed0221112f1f34261389a916f7f013c0aab7add16d

Contents?: true

Size: 635 Bytes

Versions: 11

Compression:

Stored size: 635 Bytes

Contents

require 'thread'

module SupplyDrop
  class ThreadPool
    def initialize(size)
      @size = size
      @jobs = Queue.new
      @retvals = []

      @pool = Array.new(@size) do |i|
        Thread.new do
          Thread.current[:id] = i

          catch(:exit) do
            loop do
              job, args = @jobs.pop
              @retvals << job.call(*args)
            end
          end
        end
      end
    end


    def schedule(*args, &block)
      @jobs << [block, args]
    end


    def shutdown
      @size.times do
        schedule { throw :exit }
      end

      @pool.map(&:join)
      @retvals
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
supply_drop-1.0.0.beta2 lib/supply_drop/thread_pool.rb
supply_drop-1.0.0.beta1 lib/supply_drop/thread_pool.rb
supply_drop-0.17.0 lib/supply_drop/thread_pool.rb
supply_drop-0.16.1 lib/supply_drop/thread_pool.rb
supply_drop-0.16.0 lib/supply_drop/thread_pool.rb
supply_drop-0.15.0 lib/supply_drop/thread_pool.rb
supply_drop-0.13.1 lib/supply_drop/thread_pool.rb
supply_drop-0.13.0 lib/supply_drop/thread_pool.rb
supply_drop-0.12.0 lib/supply_drop/thread_pool.rb
supply_drop-0.11.1 lib/supply_drop/thread_pool.rb
supply_drop-0.11.0 lib/supply_drop/thread_pool.rb