class Async::Barrier
@public Since ‘stable-v1`.
A synchronization primitive, which allows one task to wait for a number of other tasks to complete. It can be used in conjunction with {Semaphore}.
def async(*arguments, parent: (@parent or Task.current), **options, &block)
Execute a child task and add it to the barrier.
def async(*arguments, parent: (@parent or Task.current), **options, &block) task = parent.async(*arguments, **options, &block) @tasks << task return task end
def empty?
Whether there are any tasks being held by the barrier.
def empty? @tasks.empty? end
def initialize(parent: nil)
@parameter parent [Task | Semaphore | Nil] The parent for holding any children tasks.
Initialize the barrier.
def initialize(parent: nil) @tasks = [] @parent = parent end
def size
def size @tasks.size end
def stop
Stop all tasks held by the barrier.
def stop # We have to be careful to avoid enumerating tasks while adding/removing to it: tasks = @tasks.dup tasks.each(&:stop) end
def wait
Wait for all tasks.
def wait # TODO: This would be better with linked list. while @tasks.any? task = @tasks.first begin task.wait ensure # We don't know for sure that the exception was due to the task completion. unless task.running? # Remove the task from the waiting list if it's finished: @tasks.shift if @tasks.first == task end end end end