123456789_123456789_123456789_123456789_123456789_

Class: Concurrent::CountDownLatch

Relationships & Source Files
Inherits: Concurrent::CountDownLatchImplementation
Defined in: lib/concurrent-ruby/concurrent/atomic/count_down_latch.rb

Overview

A synchronization object that allows one thread to wait on multiple other threads. The thread that will wait creates a CountDownLatch and sets the initial value (normally equal to the number of other threads). The initiating thread passes the latch to the other threads then waits for the other threads by calling the #wait method. Each of the other threads calls #count_down when done with its work. When the latch counter reaches zero the waiting thread is unblocked and continues with its work. A CountDownLatch can be used only once. Its value cannot be reset.

Examples:

Waiter and Decrementer

latch = Concurrent::CountDownLatch.new(3)

waiter = Thread.new do
  latch.wait()
  puts ("Waiter released")
end

decrementer = Thread.new do
  sleep(1)
  latch.count_down
  puts latch.count

  sleep(1)
  latch.count_down
  puts latch.count

  sleep(1)
  latch.count_down
  puts latch.count
end

[waiter, decrementer].each(&:join)

Class Method Summary

Instance Method Summary

Constructor Details

.new(count = 1)

Create a new CountDownLatch with the initial #count.

Parameters:

  • count (new) (defaults to: 1)

    the initial count

Raises:

  • (ArgumentError)

    if #count is not an integer or is less than zero

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic/count_down_latch.rb', line 98

class CountDownLatch < CountDownLatchImplementation
end

Instance Method Details

#countFixnum

The current value of the counter.

Returns:

  • (Fixnum)

    the current value of the counter

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic/count_down_latch.rb', line 98

class CountDownLatch < CountDownLatchImplementation
end

#count_down

Signal the latch to decrement the counter. Will signal all blocked threads when the #count reaches zero.

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic/count_down_latch.rb', line 98

class CountDownLatch < CountDownLatchImplementation
end

#wait(timeout = nil) ⇒ Boolean

Block on the latch until the counter reaches zero or until timeout is reached.

Parameters:

  • timeout (Fixnum) (defaults to: nil)

    the number of seconds to wait for the counter or nil to block indefinitely

Returns:

  • (Boolean)

    true if the #count reaches zero else false on timeout

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic/count_down_latch.rb', line 98

class CountDownLatch < CountDownLatchImplementation
end