Class: Bob::BackgroundEngines::Threaded::ThreadPool

Inherits:
Object
  • Object
show all
Defined in:
lib/bob/background_engines/threaded.rb

Defined Under Namespace

Classes: Incrementor

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size = nil) ⇒ ThreadPool

Returns a new instance of ThreadPool.



69
70
71
72
73
74
# File 'lib/bob/background_engines/threaded.rb', line 69

def initialize(size = nil)
  size ||= 2
  @jobs    = Queue.new
  @njobs   = Incrementor.new
  @workers = Array.new(size) { spawn }
end

Instance Attribute Details

#jobsObject (readonly)

Returns the value of attribute jobs.



53
54
55
# File 'lib/bob/background_engines/threaded.rb', line 53

def jobs
  @jobs
end

#sizeObject

Returns the value of attribute size.



53
54
55
# File 'lib/bob/background_engines/threaded.rb', line 53

def size
  @size
end

Instance Method Details

#add(*jobs, &blk) ⇒ Object Also known as: push, <<



76
77
78
79
80
81
82
83
# File 'lib/bob/background_engines/threaded.rb', line 76

def add(*jobs, &blk)
  jobs = jobs + Array(blk)

  jobs.each do |job|
    @jobs << job
    @njobs.inc
  end
end

#njobsObject



88
89
90
# File 'lib/bob/background_engines/threaded.rb', line 88

def njobs
  @njobs.to_i
end