dsjswclzh40259075 2018-12-03 16:12
浏览 74
已采纳

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

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 {
            break
        }
    }
}

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
        default:
            break
        }
    }
}

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

    waitGroup.Add(6)

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

    go Manager(&list.List, writerChan)
    waitGroup.Wait()
}

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)
        close(writerChan)
    }
}

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条回答 默认 最新

  • dougudu3564 2018-12-03 16:40
    关注

    It looks like your logic to exit the Manager func is now different. Before you waited for the channel to be closed, however now you don't check at all. In fact Manager will never exit. This also implies that the WaitGroup will never be done.

    I think the select in Manager it not necessary. If you are closing the channel, then just range over it:

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

    This will properly exit when the writerChan is closed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵