Class: VimPK::ThreadPool

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

Instance Method Summary collapse

Constructor Details

#initialize(size = Etc.nprocessors * 2) ⇒ ThreadPool

Returns a new instance of ThreadPool.



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

def initialize(size = Etc.nprocessors * 2)
  Thread.abort_on_exception = true
  @size = size
  @jobs = Queue.new
  @workers = Array.new(size) do
    Thread.new(@jobs) do |jobs|
      while (job = jobs.pop)
        job.call
      end

      Thread.exit
    end
  end
end

Instance Method Details

#schedule(job) ⇒ Object



18
19
20
# File 'lib/vimpk/thread_pool.rb', line 18

def schedule(job)
  @jobs << job
end

#shutdownObject



22
23
24
25
# File 'lib/vimpk/thread_pool.rb', line 22

def shutdown
  @jobs.close
  @workers.each(&:join)
end