Class: Unity::Utils::ThreadPool
- Inherits:
-
Object
- Object
- Unity::Utils::ThreadPool
- Defined in:
- lib/unity/utils/thread_pool.rb
Instance Method Summary collapse
-
#initialize(size) ⇒ ThreadPool
constructor
A new instance of ThreadPool.
- #run! ⇒ Object
- #schedule(*args, &block) ⇒ Object
Constructor Details
#initialize(size) ⇒ ThreadPool
Returns a new instance of ThreadPool.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/unity/utils/thread_pool.rb', line 6 def initialize(size) @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
#run! ⇒ Object
26 27 28 29 30 31 |
# File 'lib/unity/utils/thread_pool.rb', line 26 def run! @size.times do schedule { throw :exit } end @pool.map(&:join) end |
#schedule(*args, &block) ⇒ Object
22 23 24 |
# File 'lib/unity/utils/thread_pool.rb', line 22 def schedule(*args, &block) @jobs << [block, args] end |