Class: Pool
- Inherits:
-
Object
- Object
- Pool
- Defined in:
- lib/genevalidator/pool.rb
Overview
Class that creates a thread safe pool.
Instance Method Summary collapse
-
#initialize(size) ⇒ Pool
constructor
A new instance of Pool.
- #schedule(*args, &block) ⇒ Object
- #shutdown ⇒ Object
Constructor Details
#initialize(size) ⇒ Pool
Returns a new instance of Pool.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/genevalidator/pool.rb', line 25 def initialize(size) @size = 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
#schedule(*args, &block) ⇒ Object
41 42 43 |
# File 'lib/genevalidator/pool.rb', line 41 def schedule(*args, &block) @jobs << [block, args] end |
#shutdown ⇒ Object
45 46 47 48 49 50 |
# File 'lib/genevalidator/pool.rb', line 45 def shutdown @size.times do schedule { throw :exit } end @pool.map(&:join) end |