crystal icon indicating copy to clipboard operation
crystal copied to clipboard

Add `ExecutionContext` support to `WaitGroup`

Open spuun opened this issue 7 months ago • 13 comments
trafficstars

With ExecutionContext soon being available I think it would be nice to be able to pass an optional ExecutionContext to WaitGroup in which the WG till spawn fibers.

I'd like to do something like

ec = ExecutionContext::MultiThreaded.new("workers", 4)
WaitGroup.wait(ec) do |wg|
  items.each { |i| wg.spawn worker(i) }
end

instead of

ec = ExecutionContext::MultiThreaded.new("workers", 4)
ec_wg = WaitGroup.new(1)
ec.spawn do
  WaitGroup.wait do |wg|
    items.each { |i| wg.spawn worker(i) }
  end
  ec_wg.done
end
ec_wg.wait

I guess it would be nice to be able to pass an EC to the constructor as well.

spuun avatar Apr 08 '25 06:04 spuun