- Notifications
You must be signed in to change notification settings - Fork0
Crystal WaitGroups without channels or counters
License
GrottoPress/pond
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Pond is aCrystal implementation of aWaitGroup, without channels or explicit counters.Pond automatically keeps track of all its fibers, and waits until all of them complete execution.
Add the dependency to your
shard.yml
:dependencies:pond:github:GrottoPress/pond
Run
shards install
Spawn fibers and wait on them:
require"pond"pond=Pond.new1000.timesdo |_| pond.fill { do_work }# <= Spawns fiber and passes block to itendpond.drain# <= Waits for fibers to complete
The code above is the same as:
require"pond"Pond.draindo |pond|1000.timesdo |_| pond.fill { do_work }endend# <= Drains pond automatically at the end of the block
You may spawnnested fibers:
In this case, allancestor fibers have to be added to the pond, otherwisePond can't guarantee any of them would complete.
require"pond"pond=Pond.newpond.filldo pond.filldo pond.fill { do_work }endendpond.drain
Note that, while you can fill a pond that was created in a another fiber, draining has to be done in the same fiber the pond was created in. This is to prevent potential deadlocks.
require"pond"pond=Pond.newpond.fill { do_work }spawn { pond.drain }# <= Error!
Run tests withcrystal spec -Dpreview_mt
. You may setCRYSTAL_WORKERS
environment variable withexport CRYSTAL_WORKERS=<number>
, before running tests.
- Fork it
- Switch to the
master
branch:git checkout master
- Create your feature branch:
git checkout -b my-new-feature
- Make your changes, updating changelog and documentation as appropriate.
- Commit your changes:
git commit
- Push to the branch:
git push origin my-new-feature
- Submit a newPull Request against the
GrottoPress:master
branch.
About
Crystal WaitGroups without channels or counters