Class: Concurrent::CyclicBarrier
- Inherits:
-
Synchronization::LockableObject
- Object
- Synchronization::LockableObject
- Concurrent::CyclicBarrier
- Defined in:
- lib/concurrent/atomic/cyclic_barrier.rb
Overview
A synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point.
Instance Method Summary (collapse)
-
- (Boolean) broken?
A barrier can be broken when: - a thread called the
resetmethod while at least one other thread was waiting - at least one thread timed out onwaitmethod. -
- (CyclicBarrier) initialize(parties) { ... }
constructor
Create a new
CyclicBarrierthat waits forpartiesthreads. -
- (Fixnum) number_waiting
The number of threads currently waiting on the barrier.
-
- (Fixnum) parties
The number of threads needed to pass the barrier.
-
- (nil) reset
resets the barrier to its initial state If there is at least one waiting thread, it will be woken up, the
waitmethod will return false and the barrier will be broken If the barrier is broken, this method restores it to the original state. -
- (Boolean) wait(timeout = nil)
Blocks on the barrier until the number of waiting threads is equal to
partiesor untiltimeoutis reached orresetis called If a block has been passed to the constructor, it will be executed once by the last arrived thread before releasing the others.
Constructor Details
- (CyclicBarrier) initialize(parties) { ... }
Create a new CyclicBarrier that waits for parties threads
40 41 42 43 44 45 46 |
# File 'lib/concurrent/atomic/cyclic_barrier.rb', line 40 def initialize(parties, &block) Utility::NativeInteger.ensure_integer_and_bounds parties Utility::NativeInteger.ensure_positive_and_no_zero parties super(&nil) synchronize { ns_initialize parties, &block } end |
Instance Method Details
- (Boolean) broken?
A barrier can be broken when:
- a thread called the
resetmethod while at least one other thread was waiting - at least one thread timed out on
waitmethod
A broken barrier can be restored using reset it's safer to create a new one
105 106 107 |
# File 'lib/concurrent/atomic/cyclic_barrier.rb', line 105 def broken? synchronize { @generation.status != :waiting } end |
- (Fixnum) number_waiting
Returns the number of threads currently waiting on the barrier
54 55 56 |
# File 'lib/concurrent/atomic/cyclic_barrier.rb', line 54 def number_waiting synchronize { @number_waiting } end |
- (Fixnum) parties
Returns the number of threads needed to pass the barrier
49 50 51 |
# File 'lib/concurrent/atomic/cyclic_barrier.rb', line 49 def parties synchronize { @parties } end |
- (nil) reset
resets the barrier to its initial state
If there is at least one waiting thread, it will be woken up, the wait
method will return false and the barrier will be broken
If the barrier is broken, this method restores it to the original state
95 96 97 |
# File 'lib/concurrent/atomic/cyclic_barrier.rb', line 95 def reset synchronize { ns_generation_done @generation, :reset } end |
- (Boolean) wait(timeout = nil)
Blocks on the barrier until the number of waiting threads is equal to
parties or until timeout is reached or reset is called
If a block has been passed to the constructor, it will be executed once by
the last arrived thread before releasing the others
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/concurrent/atomic/cyclic_barrier.rb', line 66 def wait(timeout = nil) synchronize do return false unless @generation.status == :waiting @number_waiting += 1 if @number_waiting == @parties @action.call if @action ns_generation_done @generation, :fulfilled true else generation = @generation if ns_wait_until(timeout) { generation.status != :waiting } generation.status == :fulfilled else ns_generation_done generation, :broken, false false end end end end |