Reputation: 61
I have a go program with multiple goroutines. I am experiencing this error and I can't figure out why. This is my code logic:
main.go
for {
...
go funcFile2()
}
Where funcFile2 is defined in File2.go and is called multiple times in a sort of loop
File2.go
var wg sync.WaitGroup
func func1(){
defer wg.Done()
return
}
func func2(){
defer wg.Done()
return
}
func func3(){
defer wg.Done()
return
}
func funcFile2(){
wg.Add(3)
go func1()
go func2()
go func3()
wg.Wait()
}
GO Version: go1.12.7
How can I solve?
Upvotes: 1
Views: 3671
Reputation: 72
To add to the accepted answer, you can call Add(1)
after another thread has already called Wait()
, as long as the total wait group count is > 0 when Add(1)
is called.
from https://pkg.go.dev/sync#WaitGroup.Add
Note that calls with a positive delta that occur when the counter is zero must happen before a Wait.
package main
import (
"fmt"
"sync"
"time"
)
func main() {
WG := sync.WaitGroup{}
WG.Add(1)
go func() {
time.Sleep(time.Second * 4)
fmt.Println("Adding while also waiting")
WG.Add(1)
WG.Done()
WG.Done()
// WG.Add(1)
}()
fmt.Println("Waiting")
WG.Wait()
fmt.Println("Returning")
}
https://play.golang.org/p/cIBeZvyngyn
stdout
Waiting
Adding while also waiting
Returning
Now, if you were to uncomment the second WG.Add(1)
line, you'll see the following runtime error
panic: sync: WaitGroup is reused before previous Wait has returned
After the second Done()
is called, the wait group counter is 0, and Wait()
has already been called by this point. So any Add(1)
operation is not allowed during this moment in time.
Upvotes: 3
Reputation: 51512
If funcfile2() is called from multiple goroutines, then they are all sharing the same WaitGroup wg, which is valid. You get this error because in one of the goroutines wg.Wait() is called, and then in another goroutine wg.Add(3) is called, which is not valid. You cannot add more to a waitgroup once it starts waiting.
You have to have a separate waitgroup for each goroutine that calls funcfile2(). If this is in a for-loop, you can do something like:
for {
wg:=sync.WaitGroup{}
go funcfile2(&wg)
}
That way each goroutine calling funcfile2 will use its own waitgroup. Of course, funcfile2 has to pass the same waitgroup to the goroutines it creates.
Upvotes: 5