This is a Crystal implementation of golang's sync.WaitGroup
There's probably still some work that needs to be done here, but it seems to work pretty well!
Add the dependency to your
dependencies: crystal-wait-group: github: jasonrobot/crystal-wait-group
To use like Go's WaitGroup, there are the
wait methods. They should work just as expected.
wg = WaitGroup.new wg.add spawn do sleep 1 wg.done end wg.wait
As a fun crystal-style addition, there is also a
spawn method which takes a block and spawns it in a fiber that is waited for by the WaitGroup.
wg = WaitGroup.new wg.spawn do sleep 1 end wg.wait
All feedback is welcome! This is my first time playing with concurrency at this level, or doing low-level Crystal stuff, so there's gotta be room for improvment. The usage of
Intrinsics.pause is taken from
Crystal::SpinLock. Basically what I've gone for here is a version of SpinLock built around an atomic counter.
git checkout -b my-new-feature)
git commit -am 'Add some feature')
git push origin my-new-feature)
Apache License 2.0