Class: Temporal::ThreadPool
- Inherits:
-
Object
- Object
- Temporal::ThreadPool
- Defined in:
- lib/temporal/thread_pool.rb
Instance Attribute Summary collapse
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
-
#initialize(size) ⇒ ThreadPool
constructor
A new instance of ThreadPool.
- #schedule(&block) ⇒ Object
- #shutdown ⇒ Object
- #wait_for_available_threads ⇒ Object
Constructor Details
#initialize(size) ⇒ ThreadPool
Returns a new instance of ThreadPool.
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/temporal/thread_pool.rb', line 12 def initialize(size) @size = size @queue = Queue.new @mutex = Mutex.new @availability = ConditionVariable.new @available_threads = size @pool = Array.new(size) do |i| Thread.new { poll } end end |
Instance Attribute Details
#size ⇒ Object (readonly)
Returns the value of attribute size.
10 11 12 |
# File 'lib/temporal/thread_pool.rb', line 10 def size @size end |
Instance Method Details
#schedule(&block) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/temporal/thread_pool.rb', line 31 def schedule(&block) @mutex.synchronize do @available_threads -= 1 @queue << block end end |
#shutdown ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/temporal/thread_pool.rb', line 38 def shutdown size.times do schedule { throw EXIT_SYMBOL } end @pool.each(&:join) end |
#wait_for_available_threads ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/temporal/thread_pool.rb', line 23 def wait_for_available_threads @mutex.synchronize do while @available_threads <= 0 @availability.wait(@mutex) end end end |