class WaitGroup

Overview

Suspend execution until a collection of fibers are finished.

The wait group is a declarative counter of how many concurrent fibers have been started. Each such fiber is expected to call #done to report that they are finished doing their work. Whenever the counter reaches zero the waiters will be resumed.

This is a simpler and more efficient alternative to using a Channel(Nil) then looping a number of times until we received N messages to resume execution.

Basic example:

require "wait_group"
wg = WaitGroup.new(5)

5.times do
  spawn do
    do_something
  ensure
    wg.done # the fiber has finished
  end
end

# suspend the current fiber until the 5 fibers are done
wg.wait

Defined in:

utils/wait_group.cr

Instance Method Summary

Instance Method Detail

def spawn(&block : -> Void) #

[View source]