我试图重写一个没有使用selector 的工作程序WaitGroup,以便它可以实现selectand WaitGroup,但是我遇到了一个问题,我找不到解决方案。似乎是 goroutine deadlock 发生了,因为Manager函数没有从 writer channel 取数据,所以 channel 被阻止发送/接收,程序被锁住。
原来的工作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()
}
所以现在的问题是,在实施之后select我WaitGroup的程序不再正常工作,它给了我一个“致命错误:goroutines 睡着了,死锁”。
也许有人可以帮我解决这个问题?我很确定问题出在函数中Manager并且是select块
白衣非少年
相关分类