尝试同步 goroutine 时出现死锁错误
Deadlock error when trying to sync goroutines
我遇到了一个恼人的问题。当我尝试使用 wg.Add()
来同步我的例程时,出现死锁错误。
package main
import (
"fmt"
"sync"
)
func hello(ch chan int, num int, wg *sync.WaitGroup) {
for {
i := <-ch
if i == num {
fmt.Println("Hello number:", i)
ch <- (num - 1)
defer wg.Done() // Same happens without defer
return
}
ch <- i
}
}
func main() {
fmt.Println("Start")
var wg sync.WaitGroup
ch := make(chan int)
for i := 0; i < 10; i++ {
wg.Add(1)
go hello(ch, i, &wg)
}
ch <- 9
wg.Wait()
fmt.Println("End")
}
输出:
Start
Hello number: 9
Hello number: 8
Hello number: 7
Hello number: 6
Hello number: 5
Hello number: 4
Hello number: 3
Hello number: 2
Hello number: 1
Hello number: 0
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [semacquire]:
sync.runtime_Semacquire(0xc04203a20c)
C:/Go/src/runtime/sema.go:47 +0x3b
sync.(*WaitGroup).Wait(0xc04203a200)
C:/Go/src/sync/waitgroup.go:131 +0x81
main.main()
C:/Users/Augusto Dias/Documents/GoLang/MT.go:34 +0x1a0
goroutine 18 [chan send]:
main.hello(0xc0420380c0, 0x0, 0xc04203a200)
C:/Users/Augusto Dias/Documents/GoLang/MT.go:13 +0x197
created by main.main
C:/Users/Augusto Dias/Documents/GoLang/MT.go:29 +0x151
exit status 2
当我在 for 块外使用 wg.Add(9)
时,没有出现错误。
func main() {
fmt.Println("Start")
var wg sync.WaitGroup
ch := make(chan int)
wg.Add(9) // Use wg.Add(10) will raise deadlock too
for i := 0; i < 10; i++ {
go hello(ch, i, &wg)
}
ch <- 9
wg.Wait()
fmt.Println("End")
}
输出:
Start
Hello number: 9
Hello number: 8
Hello number: 7
Hello number: 6
Hello number: 5
Hello number: 4
Hello number: 3
Hello number: 2
Hello number: 1
End
为什么会发生这种情况,我的意思是,为什么当我等待所有例程时例程会休眠?使用相同的发送和接收通道可能是此问题的根源?
频道 0(调用 go hello(ch, 0, &wg)
)因为它是最后一个活跃的频道,所以卡在这条线上
ch <- (num - 1)
它正在尝试发送到一个频道,但没有人在那里接收它。因此该函数将无限期地等待并且永远不会完成。
关于如何解决这个问题的一些建议
- 在主循环中创建一个消费者
- 使通道
ch
不阻塞
我遇到了一个恼人的问题。当我尝试使用 wg.Add()
来同步我的例程时,出现死锁错误。
package main
import (
"fmt"
"sync"
)
func hello(ch chan int, num int, wg *sync.WaitGroup) {
for {
i := <-ch
if i == num {
fmt.Println("Hello number:", i)
ch <- (num - 1)
defer wg.Done() // Same happens without defer
return
}
ch <- i
}
}
func main() {
fmt.Println("Start")
var wg sync.WaitGroup
ch := make(chan int)
for i := 0; i < 10; i++ {
wg.Add(1)
go hello(ch, i, &wg)
}
ch <- 9
wg.Wait()
fmt.Println("End")
}
输出:
Start
Hello number: 9
Hello number: 8
Hello number: 7
Hello number: 6
Hello number: 5
Hello number: 4
Hello number: 3
Hello number: 2
Hello number: 1
Hello number: 0
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [semacquire]:
sync.runtime_Semacquire(0xc04203a20c)
C:/Go/src/runtime/sema.go:47 +0x3b
sync.(*WaitGroup).Wait(0xc04203a200)
C:/Go/src/sync/waitgroup.go:131 +0x81
main.main()
C:/Users/Augusto Dias/Documents/GoLang/MT.go:34 +0x1a0
goroutine 18 [chan send]:
main.hello(0xc0420380c0, 0x0, 0xc04203a200)
C:/Users/Augusto Dias/Documents/GoLang/MT.go:13 +0x197
created by main.main
C:/Users/Augusto Dias/Documents/GoLang/MT.go:29 +0x151
exit status 2
当我在 for 块外使用 wg.Add(9)
时,没有出现错误。
func main() {
fmt.Println("Start")
var wg sync.WaitGroup
ch := make(chan int)
wg.Add(9) // Use wg.Add(10) will raise deadlock too
for i := 0; i < 10; i++ {
go hello(ch, i, &wg)
}
ch <- 9
wg.Wait()
fmt.Println("End")
}
输出:
Start
Hello number: 9
Hello number: 8
Hello number: 7
Hello number: 6
Hello number: 5
Hello number: 4
Hello number: 3
Hello number: 2
Hello number: 1
End
为什么会发生这种情况,我的意思是,为什么当我等待所有例程时例程会休眠?使用相同的发送和接收通道可能是此问题的根源?
频道 0(调用 go hello(ch, 0, &wg)
)因为它是最后一个活跃的频道,所以卡在这条线上
ch <- (num - 1)
它正在尝试发送到一个频道,但没有人在那里接收它。因此该函数将无限期地等待并且永远不会完成。
关于如何解决这个问题的一些建议
- 在主循环中创建一个消费者
- 使通道
ch
不阻塞