dp926460 2016-05-12 10:05
浏览 40
已采纳

Go程序陷入僵局

Here is my Golang program which I am playing with just to get my concepts right. When I run the program it is deadlocked I don't understand why ? Please anyone point out what is going wrong ?

package main

import (
    "fmt"
    "sync"
)

var wg sync.WaitGroup

func main() {

    numOfGoRoutines := 10
    wg.Add(numOfGoRoutines)
    ch := make(chan int, numOfGoRoutines)

    for i := 0; i < numOfGoRoutines; i++ {
        a := i
        go sqr(ch, a, wg)
    }
    wg.Wait()
    fmt.Println("After WAIT")
    close(ch)
    var res int
    for i := range ch {
        res += i
    }
    ch = nil
    fmt.Println("result = ", res)

}

func sqr(ch chan int, val int, wg sync.WaitGroup) {
    fmt.Println("go - ", val)
    s := val * val
    ch <- s
    wg.Done()
}

and the output is:

warning: GOPATH set to GOROOT (C:\\Go) has no effect
go -  9
go -  0
go -  1
go -  2
go -  3
go -  4
go -  5
go -  6
go -  7
go -  8
fatal error: all goroutines are asleep - deadlock!

goroutine 1 [semacquire]:
sync.runtime_Semacquire(0x5bcabc)
        C:/Go/src/runtime/sema.go:47 +0x2d
sync.(*WaitGroup).Wait(0x5bcab0)
        C:/Go/src/sync/waitgroup.go:127 +0xbb
main.main()
        C:/demo/go-work/main.go:20 +0xdf
exit status 2
  • 写回答

1条回答 默认 最新

  • dongya1228 2016-05-12 10:09
    关注

    The problem is that you're passing a copy of sync.WaitGroup to the goroutines, rather than a reference (i.e. a pointer):

    package main
    
    import (
        "fmt"
        "sync"
    )
    
    var wg sync.WaitGroup
    
    func main() {
    
        numOfGoRoutines := 10
        wg.Add(numOfGoRoutines)
        ch := make(chan int, numOfGoRoutines)
    
        for i := 0; i < numOfGoRoutines; i++ {
            a := i
            go sqr(ch, a, &wg)
        }
        wg.Wait()
        fmt.Println("After WAIT")
        close(ch)
        var res int
        for i := range ch {
            res += i
        }
        ch = nil
        fmt.Println("result = ", res)
    
    }
    
    func sqr(ch chan int, val int, wg *sync.WaitGroup) {
        fmt.Println("go - ", val)
        s := val * val
        ch <- s
        wg.Done()
    }
    

    Additionally, since wg is a global variable, you could just remove the parameter entirely:

    package main
    
    import (
        "fmt"
        "sync"
    )
    
    var wg sync.WaitGroup
    
    func main() {
    
        numOfGoRoutines := 10
        wg.Add(numOfGoRoutines)
        ch := make(chan int, numOfGoRoutines)
    
        for i := 0; i < numOfGoRoutines; i++ {
            a := i
            go sqr(ch, a)
        }
        wg.Wait()
        fmt.Println("After WAIT")
        close(ch)
        var res int
        for i := range ch {
            res += i
        }
        ch = nil
        fmt.Println("result = ", res)
    
    }
    
    func sqr(ch chan int, val int) {
        fmt.Println("go - ", val)
        s := val * val
        ch <- s
        wg.Done()
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口