Sha256: 041e2da7ef905060d30d5c1cf7984cee0e50c523dfce44681c635b87b8daf03c

Contents?: true

Size: 679 Bytes

Versions: 5

Compression:

Stored size: 679 Bytes

Contents

# -*- encoding: binary -*-
# :enddoc:
require 'thread'

# Thread pool class based on pulling off a single Ruby Queue.
# This is NOT used for the ThreadPool class, since that class does not
# need a userspace Queue.
class Rainbows::QueuePool < Struct.new(:queue, :threads)
  G = Rainbows::G

  def initialize(size = 20, &block)
    q = Queue.new
    self.threads = (1..size).map do
      Thread.new do
        while job = q.shift
          block.call(job)
        end
      end
    end
    self.queue = q
  end

  def quit!
    threads.each { |_| queue << nil }
    threads.delete_if do |t|
      G.tick
      t.alive? ? t.join(0.01) : true
    end until threads.empty?
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rainbows-2.1.0 lib/rainbows/queue_pool.rb
rainbows-2.0.1 lib/rainbows/queue_pool.rb
rainbows-2.0.0 lib/rainbows/queue_pool.rb
rainbows-1.0.0 lib/rainbows/queue_pool.rb
rainbows-1.0.0pre1 lib/rainbows/queue_pool.rb