Class: Moto::ThreadPool

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

Instance Method Summary collapse

Constructor Details

#initialize(size = 1) ⇒ ThreadPool

Returns a new instance of ThreadPool.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/thread_pool.rb', line 4

def initialize(size=1)
  @size = size
  @jobs = Queue.new
  @pool = Array.new(@size) do |i|
    Thread.new do
      Thread.current[:id] = i
      catch(:exit) do
        loop do
          job, args = @jobs.pop
          job.call(*args)
        end
      end
    end
  end
end

Instance Method Details

#schedule(*args, &block) ⇒ Object



20
21
22
# File 'lib/thread_pool.rb', line 20

def schedule(*args, &block)
  @jobs << [block, args]
end

#shutdownObject



24
25
26
27
28
29
# File 'lib/thread_pool.rb', line 24

def shutdown
  @size.times do
    schedule { throw :exit }
  end
  @pool.each(&:join)
end