douchai7891 2017-03-13 15:32
浏览 69
已采纳

尝试同步Goroutine时出现死锁错误

I'm facing an annoying problem. When I try to use wg.Add() to sync my routines, a deadlock error is raised.

package main

import (
    "fmt"
    "sync"
)

func hello(ch chan int, num int, wg *sync.WaitGroup) {
    for {
        i := <-ch
        if i == num {
            fmt.Println("Hello number:", i)
            ch <- (num - 1)
            defer wg.Done() // Same happens without defer
            return
        }
        ch <- i
    }
}

func main() {
    fmt.Println("Start")

    var wg sync.WaitGroup
    ch := make(chan int)

    for i := 0; i < 10; i++ {
        wg.Add(1)
        go hello(ch, i, &wg)
    }

    ch <- 9

    wg.Wait()

    fmt.Println("End")
}

Outputs:

Start
Hello number: 9
Hello number: 8
Hello number: 7
Hello number: 6
Hello number: 5
Hello number: 4
Hello number: 3
Hello number: 2
Hello number: 1
Hello number: 0
fatal error: all goroutines are asleep - deadlock!

goroutine 1 [semacquire]:
sync.runtime_Semacquire(0xc04203a20c)
        C:/Go/src/runtime/sema.go:47 +0x3b
sync.(*WaitGroup).Wait(0xc04203a200)
        C:/Go/src/sync/waitgroup.go:131 +0x81
main.main()
        C:/Users/Augusto Dias/Documents/GoLang/MT.go:34 +0x1a0

goroutine 18 [chan send]:
main.hello(0xc0420380c0, 0x0, 0xc04203a200)
        C:/Users/Augusto Dias/Documents/GoLang/MT.go:13 +0x197
created by main.main
        C:/Users/Augusto Dias/Documents/GoLang/MT.go:29 +0x151
exit status 2

When I use wg.Add(9) outside the for block, I got no error.

func main() {
    fmt.Println("Start")

    var wg sync.WaitGroup
    ch := make(chan int)

    wg.Add(9) // Use wg.Add(10) will raise deadlock too

    for i := 0; i < 10; i++ {
        go hello(ch, i, &wg)
    }

    ch <- 9

    wg.Wait()
    fmt.Println("End")
}

Outputs:

Start
Hello number: 9
Hello number: 8
Hello number: 7
Hello number: 6
Hello number: 5
Hello number: 4
Hello number: 3
Hello number: 2
Hello number: 1
End

Why is this happening, I mean, why routines goes asleep when I wait for them all? Use the same channel for send and receive can be the source of this problem?

  • 写回答

1条回答 默认 最新

  • dongzhuang6247 2017-03-13 15:38
    关注

    Channel 0 (the call go hello(ch, 0, &wg)) since it is the last channel alive gets stuck on this line

    ch <- (num - 1)
    

    It is attempting to send to a channel but no one is there to receive it. Thus the function will wait indefinitely and never be done.

    Some suggestions for how to remove this problem

    • create a consumer in the main loop
    • make the channel ch non blocking
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 咨询一下有关于王者荣耀赢藏战绩
  • ¥100 求购一套带接口实现实习自动签到打卡
  • ¥50 MacOS 使用虚拟机安装k8s
  • ¥500 亚马逊 COOKIE我如何才能实现 登录一个亚马逊账户 下发新 COOKIE ..我使用下发新COOKIE 导入ADS 指纹浏览器登录,我把账户密码 修改过后,原来下发新COOKIE 不会失效的方式
  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配
  • ¥15 关于#vscode#的问题:把软件卸载不会再出现蓝屏
  • ¥15 vimplus出现的错误
  • ¥15 usb无线网卡转typec口
  • ¥30 怎么使用AVL fire ESE软件自带的优化模式来优化设计Soot和NOx?