class TaskBag::Bag

Public Class Methods

new(jobs=Queue.new) click to toggle source
# File lib/taskbag/bag.rb, line 5
def initialize(jobs=Queue.new)
  @closed = true
  @jobs = jobs
  @threads = []
end
open(nworkers) click to toggle source
# File lib/taskbag/bag.rb, line 40
def self.open(nworkers)
  Bag.new.tap {|b| b.open(nworkers)}
end

Public Instance Methods

<<(object)
Alias for: add
add(object) click to toggle source
# File lib/taskbag/bag.rb, line 27
def add(object)
  @jobs.push object
end
Also aliased as: <<
close!() click to toggle source
# File lib/taskbag/bag.rb, line 20
def close!
  raise 'Bag is already closed!' if closed?
  loop { break if @jobs.empty? }
  @closed = true
  @threads.each{|t| t.join}
end
closed?() click to toggle source
# File lib/taskbag/bag.rb, line 32
def closed?
  !!@closed
end
next() click to toggle source
# File lib/taskbag/bag.rb, line 36
def next
  @jobs.pop unless @jobs.empty?
end
nworkers() click to toggle source
# File lib/taskbag/bag.rb, line 44
def nworkers
  @threads.size
end
open(nworkers) click to toggle source
# File lib/taskbag/bag.rb, line 11
def open(nworkers)
  raise "Bag is already opened!" unless closed?
  @closed = false
  _self = self
  @threads = nworkers.times.map do
    Thread.new { Worker.start(_self) }
  end
end