去程序越来越僵局 [英] Go program getting deadlock

查看:147
本文介绍了去程序越来越僵局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我正在玩的Golang程序,目的只是为了让我的概念正确。
当我运行程序时,它陷入了僵局,我不明白为什么?
请大家指出发生了什么问题?

 包主

导入(
fmt
sync


var wg sync.WaitGroup

func main(){

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

for i:= 0;我< numOfGoRoutines; i ++ {
a:= i
转到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()
}



输出结果为:

 警告: GOPATH设置为GOROOT(C:\\ Go)无效
去 - 9
去 - 0
去 - 1
去 - 2
去 - 3
去 - 4
去 - 5
去 - 6
去 - 7
去 - 8
致命错误:所有goroutines都睡着了 - 死锁!

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
退出状态2


解决方案

问题是,您正在将 sync.WaitGroup 的副本传递给goroutines,而不是引用(即指针):

 包主
$ b导入(
fmt
同步


var wg sync.WaitGroup

func main(){

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

for i:= 0;我< numOfGoRoutines; i ++ {
a:= i
转到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()
}

此外,由于 wg 是一个全局变量,因此您可以删除参数完全:

  package main 

import(
fmt
同步


var wg sync.WaitGroup

func main(){

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

for i:= 0;我< numOfGoRoutines; i ++ {
a:= i
转到sqr(ch,a)
}
wg.Wait()
fmt.Println(After WAIT)
关闭(ch)
var res int
为i:=范围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()
}


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

解决方案

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

这篇关于去程序越来越僵局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆