Class: FutureProof::ThreadPool
- Inherits:
-
Object
- Object
- FutureProof::ThreadPool
- Defined in:
- lib/future_proof/thread_pool.rb
Instance Method Summary collapse
- #finalize ⇒ Object
- #finalize! ⇒ Object
-
#initialize(size) ⇒ ThreadPool
constructor
A new instance of ThreadPool.
- #perform ⇒ Object
- #submit(*args, &block) ⇒ Object
- #values ⇒ Object
- #wait ⇒ Object
Constructor Details
#initialize(size) ⇒ ThreadPool
Returns a new instance of ThreadPool.
5 6 7 8 9 10 |
# File 'lib/future_proof/thread_pool.rb', line 5 def initialize(size) @size = size @threads = [] @queue = Queue.new @values = FutureProof::FutureQueue.new end |
Instance Method Details
#finalize ⇒ Object
33 34 35 |
# File 'lib/future_proof/thread_pool.rb', line 33 def finalize @size.times { @queue.push :END_OF_WORK } end |
#finalize! ⇒ Object
52 53 54 55 |
# File 'lib/future_proof/thread_pool.rb', line 52 def finalize! @queue.clear finalize end |
#perform ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/future_proof/thread_pool.rb', line 16 def perform unless @threads.any? { |t| t.alive? } @values.start! @size.times do @threads << Thread.new do while job = @queue.pop if job == :END_OF_WORK break else @values.push *job[1], &job[0] end end end end end end |
#submit(*args, &block) ⇒ Object
12 13 14 |
# File 'lib/future_proof/thread_pool.rb', line 12 def submit(*args, &block) @queue.push [block, args] end |
#values ⇒ Object
42 43 44 45 46 |
# File 'lib/future_proof/thread_pool.rb', line 42 def values wait @values.stop! @values.values end |
#wait ⇒ Object
37 38 39 40 |
# File 'lib/future_proof/thread_pool.rb', line 37 def wait finalize @threads.map &:join end |