dsjswclzh40259075 2018-12-03 16:12
浏览 74


I've tried to rewrite a working program which didn't use select or WaitGroup, so that it would implement select and WaitGroup, but I've came upon an issue, which I can't find a solution to. It seems that goroutine deadlock occurs, because the Manager function is not taking the data from the writer channel, therefore the channel is blocked from sending/receiving and the program locks up.

The original working Manager function, without 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 {

So I had a working program, but needed to implement WaitGroup and select, there are the updated codes :

The updated Manager function with select implemenation :

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

The updated Main function with WaitGroup implementation :

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


    for i := 0; i < len(teams); i++ {
        go Writer(teams, teams[i], writerChan, writerFinishChan)

    go Manager(&list.List, writerChan)

The Writer function which sends data to writerChan

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)

So the problem now is that after implementing select and WaitGroup my program doesn't work properly anymore, it gives me a "fatal error : goroutines asleep, deadlock".

Maybe someone can help me up with sorting out this issue? I'm quite sure that the issue lies within the Manager function and it's select block

  • 写回答

1条回答 默认 最新


      相关推荐 更多相似问题


      • ¥15 有关c++的问题,利用相关知识
      • ¥15 求香农编码和解码的matlab代码
      • ¥20 ROS中的TEB局部规划问题
      • ¥20 关于#matlab#的问题:要求测出上面图片中所有同心圆的半径
      • ¥20 epanet软件运行问题
      • ¥15 Python 文件读取
      • ¥60 dpabi进行Alff计算时脑池有干扰信号
      • ¥15 利用kmeans或kshape聚类分析对归一化的无量纲时间-降雨序列进行聚类
      • ¥15 protel99.SE提示一下弹窗
      • ¥15 银河麒麟v10 执行.run失败如何解决