Go-like WaitGroup to wait multiple coroutines to finish
See original GitHub issueHi,
This is more a question than an issue, however - are there any plans to have WaitGroup
-like mechanism? Go documentation for WaitGroup is here.
The problem it is supposed to solve is how to wait bunch of coroutines to complete? Here is a nice summary. Currently it is achievable via channels (sort of done
channel) or just by doing jobs.forEach { it.join() }
. However the latter requires extra list / array allocation whilst WaitGroup
is effectively an atomic counter wrapper with some wait
logic in it (see examples). Although it can be implemented quickly it still might be useful as a part of kotlinx.coroutines
.
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
How to Wait for All Goroutines to Finish Executing Before ...
A WaitGroup waits for a collection of goroutines to finish. The main goroutine calls Add to set the number of goroutines to wait...
Read more >How to wait for all goroutines to finish without using time.Sleep?
WaitGroup.Add docs: Note that calls with positive delta must happen before the call to Wait, or else Wait may wait for too small...
Read more >Go Concurrency Patterns: Pipelines and cancellation
Multiple functions can read from the same channel until that ... WaitGroup type provides a simple way to arrange this synchronization:.
Read more >How to wait for a goroutine to finish in Golang? - Tutorialspoint
The most important part that allows us to wait for the completion of these goroutines is the Wait() function.
Read more >Go routines for stateless AWS Lambda functions
If we were instead invoking the Lambda function multiple times in ... Println("Creating the sized wait group to synchronise the group of ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I do not immediately see how
WaitGroup
makes the code cleaner. Compare an example withWaitGroup
to an example with a list of jobs. This is a line-by-line translation:It looks cleaner and less error-prone without
WaitGroup
. In particular, the example with aWaitGroup
has a subtle bug that would reproduce only rarely (heisenbug). Under certain load, the firstwaitGroup.add()
could be immediately followed by an invocationwaitGroup.done()
, thus triggering WaitGroup’s notification prematurely. There is no risk of making this mistake with a list of jobs.Please, keep in mind that in Go there is nothing like a
Job
, so there is simply no other way to wait for a group of coroutines to finish. Go has to have something likeWaitGroup
with all its error-proneness. But we don’t have to.Also note, that in
kotlinx.corotoines
theJob
is already created with each coroutine you launch. Allocating an array to keep their list is a relatively minor constant-fraction overhead to that.Thanks for clarification, it makes sense. Good point about not having Job class in Go!