使用带有 select 的通道时的 Goroutine 死锁
Goroutine deadlock when using a channel with select
我试图重写一个没有使用 select
或 WaitGroup
的工作程序,以便它可以实现 select
和 WaitGroup
,但我遇到了一个问题,我找不到解决方案。似乎 goroutine 死锁发生了,因为 Manager
函数没有从 writer 通道获取数据,因此通道被阻塞 sending/receiving 并且程序锁定。
原始工作 Manager
功能,没有 select
:
func Manager(list *[]Request, writerChan <-chan int) {
ageIn, writersOpen := <-writerChan
for {
if writersOpen { // if writers channel is open
Add(list, Request{Value: ageIn, Count: 1}) // putting new object to list
ageIn, writersOpen = <-writerChan // receiving new player from writer channe;
} else {
break
}
}
}
所以我有一个工作程序,但需要实施 WaitGroup
和 select
,有更新的代码:
更新了 Manager
函数 和 select
实现:
func Manager(list *[]Request, writerChan <-chan int) {
defer waitGroup.Done()
for {
select {
case ageIn := <-writerChan:
Add(list, Request{Value: ageIn, Count: 1}) // add player to list
default:
break
}
}
}
更新了 Main
函数 WaitGroup
实现:
var waitGroup sync.WaitGroup
func main() {
list := ParallelList{List: make([]Request, 0)}
readers, teams, players := ReadData("data.txt")
writerChan := make(chan int) //any2one writers channel
writerFinishChan := make(chan int, 6) // channel to know when all writers are done writing
waitGroup.Add(6)
for i := 0; i < len(teams); i++ {
go Writer(teams, teams[i], writerChan, writerFinishChan)
}
go Manager(&list.List, writerChan)
waitGroup.Wait()
}
向writerChan发送数据的Writer
函数
func Writer(teams [][]Player, team []Player, writerChan chan<- int,
writerFinishChan chan int) {
defer waitGroup.Done()
count := len(team)
for i := 0; i < count; i++ {
writerChan <- team[i].Age
}
writerFinishChan <- 1 // when writer finishes writing, he puts 1 to the "writerFinishChan"
if len(writerFinishChan) == len(teams) { // if all writers are done writing (the len should be equal to 6)
close(writerChan)
}
}
所以现在的问题是,在实施 select
和 WaitGroup
之后,我的程序不再正常工作,它给了我一个 "fatal error : goroutines asleep, deadlock"。
也许有人可以帮我解决这个问题?我很确定问题出在 Manager
函数中,它是 select
块
看起来您退出 Manager
函数的逻辑现在有所不同。在您等待通道关闭之前,但是现在您根本不检查。事实上 Manager
永远不会退出。这也意味着 WaitGroup
永远不会完成。
我认为Manager
中的select没有必要。如果你要关闭频道,那么就在它上面范围:
for ageIn := range writerChan {
Add(list, Request{Value: ageIn, Count: 1}) // putting new object to list
}
当 writerChan
关闭时,这将正确退出。
我试图重写一个没有使用 select
或 WaitGroup
的工作程序,以便它可以实现 select
和 WaitGroup
,但我遇到了一个问题,我找不到解决方案。似乎 goroutine 死锁发生了,因为 Manager
函数没有从 writer 通道获取数据,因此通道被阻塞 sending/receiving 并且程序锁定。
原始工作 Manager
功能,没有 select
:
func Manager(list *[]Request, writerChan <-chan int) {
ageIn, writersOpen := <-writerChan
for {
if writersOpen { // if writers channel is open
Add(list, Request{Value: ageIn, Count: 1}) // putting new object to list
ageIn, writersOpen = <-writerChan // receiving new player from writer channe;
} else {
break
}
}
}
所以我有一个工作程序,但需要实施 WaitGroup
和 select
,有更新的代码:
更新了 Manager
函数 和 select
实现:
func Manager(list *[]Request, writerChan <-chan int) {
defer waitGroup.Done()
for {
select {
case ageIn := <-writerChan:
Add(list, Request{Value: ageIn, Count: 1}) // add player to list
default:
break
}
}
}
更新了 Main
函数 WaitGroup
实现:
var waitGroup sync.WaitGroup
func main() {
list := ParallelList{List: make([]Request, 0)}
readers, teams, players := ReadData("data.txt")
writerChan := make(chan int) //any2one writers channel
writerFinishChan := make(chan int, 6) // channel to know when all writers are done writing
waitGroup.Add(6)
for i := 0; i < len(teams); i++ {
go Writer(teams, teams[i], writerChan, writerFinishChan)
}
go Manager(&list.List, writerChan)
waitGroup.Wait()
}
向writerChan发送数据的Writer
函数
func Writer(teams [][]Player, team []Player, writerChan chan<- int,
writerFinishChan chan int) {
defer waitGroup.Done()
count := len(team)
for i := 0; i < count; i++ {
writerChan <- team[i].Age
}
writerFinishChan <- 1 // when writer finishes writing, he puts 1 to the "writerFinishChan"
if len(writerFinishChan) == len(teams) { // if all writers are done writing (the len should be equal to 6)
close(writerChan)
}
}
所以现在的问题是,在实施 select
和 WaitGroup
之后,我的程序不再正常工作,它给了我一个 "fatal error : goroutines asleep, deadlock"。
也许有人可以帮我解决这个问题?我很确定问题出在 Manager
函数中,它是 select
块
看起来您退出 Manager
函数的逻辑现在有所不同。在您等待通道关闭之前,但是现在您根本不检查。事实上 Manager
永远不会退出。这也意味着 WaitGroup
永远不会完成。
我认为Manager
中的select没有必要。如果你要关闭频道,那么就在它上面范围:
for ageIn := range writerChan {
Add(list, Request{Value: ageIn, Count: 1}) // putting new object to list
}
当 writerChan
关闭时,这将正确退出。