Sha256: fc11c1984d0595bccddd2e6eb27e1ede26a284213e292c713be5c5b052b1eeda

Contents?: true

Size: 614 Bytes

Versions: 1

Compression:

Stored size: 614 Bytes

Contents

require 'thread'

module Outlander

  class ThreadsPool

    def initialize(num_threads = 3)
      @num_threads = num_threads
      @queue = Queue.new
    end

    def enqueue(&task)
      @queue << task
    end

    def start
      raise "Could not start with empty queue" if @queue.empty?
      run_threads @num_threads
      sleep 1 until @queue.empty? && @queue.num_waiting == @num_threads
    end

    private

    def run_threads(num_threads = 1)
      1.upto(num_threads) do
        Thread.new do
          while task = @queue.pop
            task.call
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
outlander-0.2.0 lib/outlander/threads_pool.rb