Sha256: dbd55c05dcd09d173fb2c67cfedf30f3e5d7ea21af9cd20729f9a15f7d74eecb

Contents?: true

Size: 715 Bytes

Versions: 2

Compression:

Stored size: 715 Bytes

Contents

module ConfCtl
  class ParallelExecutor
    attr_reader :thread_count

    def initialize(threads)
      @thread_count = threads
      @threads = []
      @queue = Queue.new
      @retvals = []
      @mutex = Mutex.new
    end

    def add(&block)
      queue << block
    end

    def run
      thread_count.times do
        threads << Thread.new { worker }
      end

      threads.each(&:join)
      retvals
    end

    protected

    attr_reader :threads, :queue, :mutex, :retvals

    def worker
      loop do
        begin
          block = queue.pop(true)
        rescue ThreadError
          return
        end

        v = block.call
        mutex.synchronize { retvals << v }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
confctl-2.0.0 lib/confctl/parallel_executor.rb
confctl-1.0.0 lib/confctl/parallel_executor.rb