Class: TaskBag::Bag
- Inherits:
-
Object
- Object
- TaskBag::Bag
- Defined in:
- lib/taskbag/bag.rb
Class Method Summary collapse
Instance Method Summary collapse
- #add(object) ⇒ Object (also: #<<)
- #close! ⇒ Object
- #closed? ⇒ Boolean
-
#initialize(jobs = Queue.new) ⇒ Bag
constructor
A new instance of Bag.
- #next ⇒ Object
- #nworkers ⇒ Object
- #open(nworkers) ⇒ Object
Constructor Details
#initialize(jobs = Queue.new) ⇒ Bag
5 6 7 8 9 |
# File 'lib/taskbag/bag.rb', line 5 def initialize(jobs=Queue.new) @closed = true @jobs = jobs @threads = [] end |
Class Method Details
.open(nworkers) ⇒ Object
40 41 42 |
# File 'lib/taskbag/bag.rb', line 40 def self.open(nworkers) Bag.new.tap {|b| b.open(nworkers)} end |
Instance Method Details
#add(object) ⇒ Object Also known as: <<
27 28 29 |
# File 'lib/taskbag/bag.rb', line 27 def add(object) @jobs.push object end |
#close! ⇒ Object
20 21 22 23 24 25 |
# 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? ⇒ Boolean
32 33 34 |
# File 'lib/taskbag/bag.rb', line 32 def closed? !!@closed end |
#next ⇒ Object
36 37 38 |
# File 'lib/taskbag/bag.rb', line 36 def next @jobs.pop unless @jobs.empty? end |
#nworkers ⇒ Object
44 45 46 |
# File 'lib/taskbag/bag.rb', line 44 def nworkers @threads.size end |
#open(nworkers) ⇒ Object
11 12 13 14 15 16 17 18 |
# 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 |