Class: VimPK::ThreadPool
- Inherits:
-
Object
- Object
- VimPK::ThreadPool
- Defined in:
- lib/vimpk/thread_pool.rb
Instance Method Summary collapse
-
#initialize(size = Etc.nprocessors * 2) ⇒ ThreadPool
constructor
A new instance of ThreadPool.
- #schedule(job) ⇒ Object
- #shutdown ⇒ Object
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 |
#shutdown ⇒ Object
22 23 24 25 |
# File 'lib/vimpk/thread_pool.rb', line 22 def shutdown @jobs.close @workers.each(&:join) end |