class Concurrent::MutexCountDownLatch
with its work. A ‘CountDownLatch` can be used only once. Its value cannot be reset.
When the latch counter reaches zero the waiting thread is unblocked and continues
method. Each of the other threads calls `#count_down` when done with its work.
latch to the other threads then waits for the other threads by calling the `#wait`
(normally equal to the number of other threads). The initiating thread passes the
The thread that will wait creates a `CountDownLatch` and sets the initial value
A synchronization object that allows one thread to wait on multiple other threads.
@!macro [attach] count_down_latch
def count
-
(Fixnum)- the current value of the counter
def count @mutex.synchronize { @count } end
def count_down
Signal the latch to decrement the counter. Will signal all blocked threads when
@!macro [attach] count_down_latch_method_count_down
def count_down @mutex.synchronize do @count -= 1 if @count > 0 @condition.broadcast if @count == 0 end end
def initialize(count)
-
(ArgumentError)- if `count` is not an integer or is less than zero
Parameters:
-
count(Fixnum) -- the initial count
def initialize(count) unless count.is_a?(Fixnum) && count >= 0 raise ArgumentError.new('count must be in integer greater than or equal zero') end @mutex = Mutex.new @condition = Condition.new @count = count end
def wait(timeout = nil)
-
(Boolean)- `true` if the `count` reaches zero else false on `timeout`
Parameters:
-
timeout(Fixnum) -- the number of seconds to wait for the counter or `nil`
def wait(timeout = nil) @mutex.synchronize do remaining = Condition::Result.new(timeout) while @count > 0 && remaining.can_wait? remaining = @condition.wait(@mutex, remaining.remaining_time) end @count == 0 end end