class

WaitGroup

Inherits Reference / Object

WaitGroup can be used to wait for a number of different fibers to complete.

Multiple fibers can be waited for, and multiple fibers can be waiting. Once there are no fibers being waited for

wg = WaitGroup.new
wg.spawn do
  sleep 1
  puts "slept for 1"
end

wg.add
spawn do
  sleep 2
  puts "slept for 2"
  wg.done
end

wg.wait
puts "done waiting"

Instance methods

add(delta = 1)

Add delta to the number of fibers being waited for.

Delta should be positive. Use done instead to decrement the counter.

Source
done

Decrease the number of fibers being waited for by 1.

This should be called from within the spawned fiber that's being waited for. Once there are 0 fibers being waited for, all waiting fibers will be restarted.

Raises an error if the counter goes below 0.

Source
spawn

Spawn a block in a fiber, calling add before it is spawned, and done once it completes.

Source
unwait
Source
wait

Pause the current fiber until there are no fibers being waited for.

If the counter is 0, wait will return immediately without blocking. Multiple fibers can wait on the same wait group.

Source