class Concurrent::CyclicBarrier
# here we can be sure that all jobs are processed
process.call 2
# use main as well
end
Thread.new(i, &process)
threads = 2.times.map do |i|
end
barrier.wait
# wait for others to finish<br>jobs.call
# execute job
barrier.wait
# waiting to start at the same time
process = -> (i) do
jobs = Array.new(3) { |i| -> { sleep i; p done: i } }
barrier = Concurrent::CyclicBarrier.new(3)
@example
other to reach a common barrier point.
A synchronization aid that allows a set of threads to all wait for each
def broken?
-
(Boolean)
- true if the barrier is broken otherwise false
def broken? synchronize { @generation.status != :waiting } end
def initialize(parties, &block)
-
(ArgumentError)
- if `parties` is not an integer or is less than zero
Other tags:
- Yield: - an optional block that will be executed that will be executed after
Parameters:
-
parties
(Fixnum
) -- the number of parties
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
def ns_generation_done(generation, status, continue = true)
def ns_generation_done(generation, status, continue = true) generation.status = status ns_next_generation if continue ns_broadcast end
def ns_initialize(parties, &block)
def ns_initialize(parties, &block) @parties = parties @action = block ns_next_generation end
def ns_next_generation
def ns_next_generation @generation = Generation.new(:waiting) @number_waiting = 0 end
def number_waiting
-
(Fixnum)
- the number of threads currently waiting on the barrier
def number_waiting synchronize { @number_waiting } end
def parties
-
(Fixnum)
- the number of threads needed to pass the barrier
def parties synchronize { @parties } end
def reset
-
(nil)
-
def reset synchronize { ns_generation_done @generation, :reset } end
def wait(timeout = nil)
-
(Boolean)
- `true` if the `count` reaches zero else false on
Parameters:
-
timeout
(Fixnum
) -- the number of seconds to wait for the counter or
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