question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Go-like WaitGroup to wait multiple coroutines to finish

See original GitHub issue

Hi, 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:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

14reactions
elizarovcommented, May 10, 2017

I do not immediately see how WaitGroup makes the code cleaner. Compare an example with WaitGroup to an example with a list of jobs. This is a line-by-line translation:

fun test() = publish(CommonPool) {
    val output = Channel<String>()
    val jobs = mutableListOf<Job>() // replaces waitGroup

    // start coroutine per thing
    listOfThings.consumeEach {
        jobs += launch(context) { // replaces waitGroup.add(), no need for waitGroup.done() 
            doSomething(it, output)
        }
    }

    launch(context) {
        for (t in output) {
            send(t)
        }
    }

    // wait for all the coroutines to finish
   jobs.forEach { it.await() } // replaces waitGroup.wait() 
}

It looks cleaner and less error-prone without WaitGroup. In particular, the example with a WaitGroup has a subtle bug that would reproduce only rarely (heisenbug). Under certain load, the first waitGroup.add() could be immediately followed by an invocation waitGroup.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 like WaitGroup with all its error-proneness. But we don’t have to.

Also note, that in kotlinx.corotoines the Job is already created with each coroutine you launch. Allocating an array to keep their list is a relatively minor constant-fraction overhead to that.

0reactions
alek-syscommented, May 10, 2017

Thanks for clarification, it makes sense. Good point about not having Job class in Go!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found