Class: Pool

Inherits:
Object
  • Object
show all
Defined in:
lib/sequenceserver/pool.rb

Overview

Simple thread pool [1].

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ Pool

Returns a new instance of Pool.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sequenceserver/pool.rb', line 23

def initialize(size)
  @size = Integer size
  @jobs = Queue.new
  @pool = Array.new(@size) do |i|
    Thread.new do
      Thread.current[:id] = i
      catch(:exit) do
        loop do
          job, args = @jobs.pop
          job.call(*args)
        end
      end
    end
  end
end

Instance Method Details

#queue(*args, &block) ⇒ Object



39
40
41
# File 'lib/sequenceserver/pool.rb', line 39

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

#shutdownObject



43
44
45
46
47
48
# File 'lib/sequenceserver/pool.rb', line 43

def shutdown
  @size.times do
    queue { throw :exit }
  end
  @pool.map(&:join)
end