Class: Infrastruct::ThreadPool
- Inherits:
-
Object
- Object
- Infrastruct::ThreadPool
- Defined in:
- lib/infrastruct/thread_pool.rb
Instance Attribute Summary collapse
-
#queue ⇒ Object
readonly
Returns the value of attribute queue.
Instance Method Summary collapse
- #enqueue(args) ⇒ Object
-
#initialize(threads:) ⇒ ThreadPool
constructor
A new instance of ThreadPool.
- #run(&block) ⇒ Object
Constructor Details
#initialize(threads:) ⇒ ThreadPool
Returns a new instance of ThreadPool.
5 6 7 8 |
# File 'lib/infrastruct/thread_pool.rb', line 5 def initialize(threads:) @queue = Infrastruct::NonblockingQueue.new @number_of_threads = threads end |
Instance Attribute Details
#queue ⇒ Object (readonly)
Returns the value of attribute queue.
3 4 5 |
# File 'lib/infrastruct/thread_pool.rb', line 3 def queue @queue end |
Instance Method Details
#enqueue(args) ⇒ Object
10 11 12 |
# File 'lib/infrastruct/thread_pool.rb', line 10 def enqueue(args) @queue.push(args) end |
#run(&block) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/infrastruct/thread_pool.rb', line 14 def run(&block) @threads = @number_of_threads.times.map do Thread.new do begin while params = @queue.pop do block.call(params) end rescue ThreadError => error raise error unless error. == 'queue empty' end end end @threads.map(&:join) end |