Class: Spandx::Core::ThreadPool

Inherits:
Object
  • Object
show all
Defined in:
lib/spandx/core/thread_pool.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size: 1) ⇒ ThreadPool

Returns a new instance of ThreadPool.



6
7
8
9
10
# File 'lib/spandx/core/thread_pool.rb', line 6

def initialize(size: 1)
  @size = size
  @queue = Queue.new
  @pool = size.times.map { start_worker_thread(@queue) }
end

Class Method Details

.open(**args) ⇒ Object



28
29
30
31
32
33
# File 'lib/spandx/core/thread_pool.rb', line 28

def self.open(**args)
  pool = new(**args)
  yield pool
ensure
  pool.shutdown
end

Instance Method Details

#done?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/spandx/core/thread_pool.rb', line 16

def done?
  @queue.empty?
end

#run(*args, &job) ⇒ Object



12
13
14
# File 'lib/spandx/core/thread_pool.rb', line 12

def run(*args, &job)
  @queue.enq([job, args])
end

#shutdownObject



20
21
22
23
24
25
26
# File 'lib/spandx/core/thread_pool.rb', line 20

def shutdown
  @size.times do
    run { throw :exit }
  end

  @pool.map(&:join)
end