登录
首页 >  Golang >  Go问答

所有的 goroutine 都在休眠

来源:stackoverflow

时间:2024-03-04 12:45:26 459浏览 收藏

在Golang实战开发的过程中,我们经常会遇到一些这样那样的问题,然后要卡好半天,等问题解决了才发现原来一些细节知识点还是没有掌握好。今天golang学习网就整理分享《所有的 goroutine 都在休眠》,聊聊,希望可以帮助到正在努力赚钱的你。

问题内容

我编写了一些旨在使用通道进行同步的代码。

var counter int64  // shared resource

    var wg sync.WaitGroup

    func main() {
        ch := make(chan int64)

        wg.Add(2)

        go incCounter(ch)
        go incCounter(ch)

        ch <- counter

        wg.Wait()
        fmt.Println("Final Counter:", counter) // expected value is 4
    }

    func incCounter(ch chan int64) {
        defer wg.Done()

        for count := 0; count < 2; count++ {
            value := <-ch
            value++
            counter = value
            ch <- counter
        }
    }

当我运行这个程序时,发生了一个错误:all goroutine 正在睡觉 - 死锁!。但是我无法解决问题,也不知道出了什么问题。有人可以帮忙吗?


解决方案


通道 make(chan int) 的隐式大小为零(参考:https://golang.org/ref/spec#Making_slices_maps_and_channels

大小为零的通道是无缓冲的。指定大小的通道 make(chan int, n) 被缓冲。有关缓冲通道与非缓冲通道的讨论,请参阅 http://golang.org/ref/spec#Send_statementshttp://play.golang.org/p/VZAiN1V8-P 处的示例说明了其中的差异。

此处,通道 <-chch <- 将被阻止,直到有人处理它(并发)。如果你用笔和纸尝试一下这个程序的流程,你就会明白为什么它被阻止了。下图显示了通过通道ch可能的数据流:

因此,如果您将 ch := make(chan int64) 改为 ch := make(chan int64,1),它将起作用。

var counter int64 // shared resource
var wg sync.waitgroup

func main() {
    ch := make(chan int64, 1)

    wg.add(2)

    go inccounter(ch)
    go inccounter(ch)

    ch <- counter

    wg.wait()
    fmt.println("final counter:", counter) // expected value is 4
}

func inccounter(ch chan int64) {
    defer wg.done()

    for count := 0; count < 2; count++ {
        value := <-ch
        value++
        counter = value
        ch <- counter
    }
}

如果我们分析一下使用 ch := make(chan int64) 时程序是如何工作的,我们可以看到该程序中有一个 goroutine 被阻塞(另一个 goroutine 被退出)。借助 time.sleep(n) 并从阻塞的 go 例程中的通道接收最后的数据,我们可以克服死锁。请参阅下面的代码:

var counter int64 // shared resource
var wg sync.WaitGroup

func main() {
    ch := make(chan int64)

    wg.Add(2)

    go incCounter(ch)
    go incCounter(ch)

    ch <- counter

    // to ensure one go routine 'incCounter' is completed and one go routine is blocked for unbuffered channel
    time.Sleep(3*time.Second)

    <-ch // to unblock the last go routine

    wg.Wait()
    fmt.Println("Final Counter:", counter) // expected value is 4
}

func incCounter(ch chan int64) {
    defer wg.Done()

    for count := 0; count < 2; count++ {
        value := <-ch
        value++
        counter = value
        ch <- counter
    }
}

到这里,我们也就讲完了《所有的 goroutine 都在休眠》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>