Class: Tins::Limited
Instance Attribute Summary collapse
-
#maximum ⇒ Object
readonly
The maximum number of worker threads.
Instance Method Summary collapse
-
#execute ⇒ Object
Execute maximum number of threads in parallel.
-
#initialize(maximum) ⇒ Limited
constructor
Create a Limited instance, that runs maximum threads at most.
Constructor Details
#initialize(maximum) ⇒ Limited
Create a Limited instance, that runs maximum threads at most.
6 7 8 9 10 11 12 |
# File 'lib/tins/limited.rb', line 6 def initialize(maximum) @mutex = Mutex.new @continue = ConditionVariable.new @maximum = Integer(maximum) raise ArgumentError, "maximum < 1" if @maximum < 1 @count = 0 end |
Instance Attribute Details
#maximum ⇒ Object (readonly)
The maximum number of worker threads.
15 16 17 |
# File 'lib/tins/limited.rb', line 15 def maximum @maximum end |
Instance Method Details
#execute ⇒ Object
Execute maximum number of threads in parallel.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/tins/limited.rb', line 18 def execute @mutex.synchronize do loop do if @count < @maximum @count += 1 Thread.new do yield @mutex.synchronize { @count -= 1 } @continue.signal end return else @continue.wait(@mutex) end end end end |