Skip to content

Add WaitGroup.wait and WaitGroup#spawn#14837

Merged
straight-shoota merged 2 commits intocrystal-lang:masterfrom
jgaskins:wait_group-wait-block
Aug 20, 2024
Merged

Add WaitGroup.wait and WaitGroup#spawn#14837
straight-shoota merged 2 commits intocrystal-lang:masterfrom
jgaskins:wait_group-wait-block

Conversation

@jgaskins
Copy link
Contributor

Fixes #14820

jgaskins added 2 commits July 27, 2024 11:58
This commit allows for usage of WaitGroup in a way that is significantly
more readable.

Currently, using this API looks like this:

    wg = WaitGroup.new(2)
    spawn do
      http.get "/foo"
    ensure
      wg.done
    end
    spawn do
      http.get "/bar"
    ensure
      wg.done
    end
    wg.wait

With this commit, the code above can be simplified to this:

    WaitGroup.wait do |wg|
      wg.spawn { http.get "/foo" }
      wg.spawn { http.get "/bar" }
    end
@stakach
Copy link
Contributor

stakach commented Jul 28, 2024

I definitely think this should be included as part of the std lib
super simple and easy to maintain

Copy link
Member

@straight-shoota straight-shoota left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you please add some doc comments (ideally with usage examples)?

@jgaskins
Copy link
Contributor Author

jgaskins commented Aug 6, 2024

Added

@straight-shoota straight-shoota added this to the 1.14.0 milestone Aug 19, 2024
@straight-shoota straight-shoota changed the title Allow WaitGroup usage to be more readable Add WaitGroup.wait and WaitGroup#spawn Aug 20, 2024
@straight-shoota straight-shoota merged commit 1baf3a7 into crystal-lang:master Aug 20, 2024
@jgaskins jgaskins deleted the wait_group-wait-block branch August 25, 2024 19:15
@crysbot
Copy link
Collaborator

crysbot commented Aug 26, 2024

This pull request has been mentioned on Crystal Forum. There might be relevant details there:

https://forum.crystal-lang.org/t/is-this-the-only-way-to-terminate/7135/2

@zw963
Copy link
Contributor

zw963 commented Oct 12, 2024

I consider this new usage is really nice.

jgaskins added a commit to jgaskins/http_client that referenced this pull request Sep 2, 2025
This was upstreamed into Crystal:
crystal-lang/crystal#14837
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Streamlining the WaitGroup API

7 participants