Sha256: e81bdb9203a2161d82c292a9c9d58c96ea608c075ba1c7cfe7225a291c5b23bb

Contents?: true

Size: 685 Bytes

Versions: 1

Compression:

Stored size: 685 Bytes

Contents

# frozen_string_literal: true

module Spandx
  module Core
    class ThreadPool
      def initialize(size: Etc.nprocessors)
        @size = size
        @jobs = Queue.new
        @pool = size.times { start_worker_thread }
      end

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

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

        @pool.map(&:join)
      end

      private

      def start_worker_thread
        Thread.new do
          catch(:exit) do
            loop do
              job, args = @jobs.deq
              job.call(*args)
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
spandx-0.12.3 lib/spandx/core/thread_pool.rb