Class: ParticlePool::Pool

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

Instance Method Summary collapse

Constructor Details

#initializePool

Returns a new instance of Pool.



2
3
4
5
6
7
# File 'lib/particle_pool/pool.rb', line 2

def initialize
  @mtx         = Mutex.new
  @threads     = []
  @round_robin = false
  @iter        = @threads.each
end

Instance Method Details

#<<(t, *args, **kwargs) ⇒ Object



9
10
11
# File 'lib/particle_pool/pool.rb', line 9

def <<(t, *args, **kwargs)
  push(t, *args, **kwargs)
end

#push(t, *args, **kwargs) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/particle_pool/pool.rb', line 13

def push(t, *args, **kwargs)
  @mtx.synchronize do
    thr = @threads.min
    raise 'no threads available' unless t
    thr.push(t, *args, **kwargs)
  end
end

#start(size = Etc.nprocessors) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/particle_pool/pool.rb', line 21

def start(size = Etc.nprocessors)
  size.times do
    @threads << ParticlePool::PoolThread.new
  end
  @threads.each do |t|
    Thread.new do
      t.start
    end
  end
end