使用带有select的通道时出现Goroutine死锁

时间:2018-12-03 16:12:54

标签: go select deadlock channel goroutine

我试图重写一个不使用selectWaitGroup的工作程序,以便它可以实现selectWaitGroup,但是我已经遇到一个问题,我找不到解决方案。似乎发生了goroutine死锁,因为Manager函数没有从writer通道获取数据,因此该通道被阻止发送/接收,并且程序被锁定。

原始工作的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
        }
    }
}

所以我有一个工作程序,但是需要实现WaitGroupselect,这里有更新的代码:

更新后的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)
    }
}

所以现在的问题是,在实现selectWaitGroup之后,我的程序无法正常工作,它给了我一个“致命错误:goroutines睡眠,死锁”。

也许有人可以帮我解决这个问题?我非常确定问题出在Manager函数之内,并且是select

1 个答案:

答案 0 :(得分:2)

现在退出Manager函数的逻辑似乎有所不同。在等待关闭通道之前,但是现在您根本不需要检查。实际上Manager将永远不会退出。这也意味着WaitGroup将永远不会完成。

我认为没有必要在Manager中进行选择。如果您要关闭频道,则只需将其覆盖范围即可:

for ageIn := range writerChan {
  Add(list, Request{Value: ageIn, Count: 1}) // putting new object to list
}

writerChan关闭时,这将正确退出。