class WaitGroup

Overview

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"

Defined in:

crystal-wait-group.cr

Instance Method Summary

Instance Method Detail

def add(delta = 1) #

Add delta to the number of fibers being waited for.

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


[View source]
def 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.


[View source]
def spawn(&block) #

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


[View source]
def unwait #

[View source]
def 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.


[View source]