为什么我的golang频道引发死锁错误? [英] Why my golang channel raise dead lock error?

查看:153
本文介绍了为什么我的golang频道引发死锁错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 包主
$ b导入(
fmt
同步


func push(c chan int,wg sync.WaitGroup){
for i:= 0;我< 5; i ++ {
c< - i
}
wg.Done()
}

func pull(c chan int,wg sync.WaitGroup){
for i:= 0;我< 5; i ++ {
result,ok:=< - c
fmt.Println(result,ok)
}
wg.Done()
}

func main(){
var wg sync.WaitGroup
wg.Add(2)
c:= make(chan int)

go push( c,wg)
go拉(c,wg)

wg.Wait()
}

输出:

  localhost:src kuankuan $ go run goroutine.go 
0 true
1 true
2 true
3 true
4 true
throw:所有goroutines都睡着了 - 死锁!

goroutine 1 [semacquire]:
sync.runtime_Semacquire(0x42130100,0x42130100)
/usr/local/go/src/pkg/runtime/zsema_amd64.c:146 + 0x25 $ * $ b sync。(* WaitGroup).Wait(0x42120420,0x0)
/usr/local/go/src/pkg/sync/waitgroup.go:79+ 0xf2
main.main()
/Users/kuankuan/go/src/goroutine.go:31 + 0xb9

goroutine 2 [syscall]:
由runtime.main创建
/ usr / local / go / src / pkg / runtime / proc.c:221
退出状态2




当你将WaitGroup传递给你的时候函数,您需要传递指针而不是值。否则,将使用WaitGroup的副本。



这是您的工作示例:

 < 











$ func push(c chan int ,wg * sync.WaitGroup){
for i:= 0;我< 5; i ++ {
c< - i
}
wg.Done()
}

func pull(c chan int,wg * sync.WaitGroup) {
for i:= 0;我< 5; i ++ {
result,ok:=< - c
fmt.Println(result,ok)
}
wg.Done()
}

func main(){
var wg sync.WaitGroup
wg.Add(2)
c:= make(chan int)

go push( c,& wg)
去拉(c,&wg)

wg.Wait()
}
pre>

package main

import (
    "fmt"
    "sync"
)

func push(c chan int,wg sync.WaitGroup) {
    for i := 0; i < 5; i++ {
        c <- i
    }
    wg.Done()
}

func pull(c chan int,wg sync.WaitGroup) {
    for i := 0; i < 5; i++ {
        result,ok := <- c
        fmt.Println(result,ok)
    }
    wg.Done()
}

func main() {
    var wg sync.WaitGroup
    wg.Add(2)
    c := make(chan int)

    go push(c,wg)
    go pull(c,wg)

    wg.Wait()
}

Output:

localhost:src kuankuan$ go run goroutine.go 
0 true
1 true
2 true
3 true
4 true
throw: all goroutines are asleep - deadlock!

goroutine 1 [semacquire]:
sync.runtime_Semacquire(0x42130100, 0x42130100)
    /usr/local/go/src/pkg/runtime/zsema_amd64.c:146 +0x25
sync.(*WaitGroup).Wait(0x42120420, 0x0)
    /usr/local/go/src/pkg/sync/waitgroup.go:79 +0xf2
main.main()
    /Users/kuankuan/go/src/goroutine.go:31 +0xb9

goroutine 2 [syscall]:
created by runtime.main
    /usr/local/go/src/pkg/runtime/proc.c:221
exit status 2

解决方案

The reason why it deadlocks is because structs are passed by value and not by reference.

When you pass the WaitGroup to your functions, you need to pass the pointer and not the value. Otherwise a copy of the WaitGroup will be used.

This is your working example:

package main

import (
    "fmt"
    "sync"
)

func push(c chan int,wg *sync.WaitGroup) {
    for i := 0; i < 5; i++ {
        c <- i
    }
    wg.Done()
}

func pull(c chan int,wg *sync.WaitGroup) {
    for i := 0; i < 5; i++ {
        result,ok := <- c
        fmt.Println(result,ok)
    }
    wg.Done()
}

func main() {
    var wg sync.WaitGroup
    wg.Add(2)
    c := make(chan int)

    go push(c,&wg)
    go pull(c,&wg)

    wg.Wait()
}

这篇关于为什么我的golang频道引发死锁错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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