登录
首页 >  Golang >  Go问答

处理生产者和消费者代码中的死锁问题的方法

来源:stackoverflow

时间:2024-02-14 23:18:28 327浏览 收藏

本篇文章向大家介绍《处理生产者和消费者代码中的死锁问题的方法》,主要包括,具有一定的参考价值,需要的朋友可以参考一下。

问题内容

当我运行下面的程序时,出现错误

davecheney      tweets about golang
beertocode      does not tweet about golang
ironzeb         tweets about golang
beertocode      tweets about golang
vampirewalk666  tweets about golang
fatal error: all goroutines are asleep - deadlock!

goroutine 1 [semacquire]:
sync.runtime_semacquire(0xc000010260?)
        /usr/local/go/src/runtime/sema.go:56 +0x25
sync.(*waitgroup).wait(0x100c000058058?)
        /usr/local/go/src/sync/waitgroup.go:136 +0x52
main.main()
        /home/joe/go/src/github.com/go-concurrency-exercises/1-producer-consumer/main.go:53 +0x14f

死锁从何而来?如何改进程序以避免死锁?

package main

import (
    "fmt"
    "sync"
    "time"
)

func producer(stream Stream, tweetChan chan *Tweet) {
    for {
        tweet, err := stream.Next()
        if err == ErrEOF {
            close(tweetChan)
            return
        }
        tweetChan <- tweet
        //tweets = append(tweets, tweet)
    }
}

func consumer(tweetChan chan *Tweet) {
    for t := range tweetChan {
        if t.IsTalkingAboutGo() {
            fmt.Println(t.Username, "\ttweets about golang")
        } else {
            fmt.Println(t.Username, "\tdoes not tweet about golang")
        }
    }
}

func main() {
    start := time.Now()
    stream := GetMockStream()

    var wg sync.WaitGroup
    tweetChan := make(chan *Tweet)
    // Producer
    //tweets := producer(stream)
    wg.Add(2)
    go producer(stream, tweetChan)
    // Consumer
    //consumer(tweets)
    go consumer(tweetChan)

    wg.Wait()

    fmt.Printf("Process took %s\n", time.Since(start))
}

如果需要查看mockstream.go,请参考 https://github.com/loong/go-concurrency-exercises/tree/master/1- producer-consumer

我的程序是原程序通过修改main.go的并发版本


正确答案


对 wg.wait() 的调用正在等待,直到组的计数器为零,但没有正在运行的 goroutine 来递减计数器。

在从 goroutine 函数返回之前调用 wg.done() 进行修复:

func producer(wg *sync.WaitGroup, stream Stream, tweetChan chan *Tweet) {
    defer wg.Done()
    for {
        tweet, err := stream.Next()
        if err == ErrEOF {
            close(tweetChan)
            return
        }
        tweetChan <- tweet
    }
}

func consumer(wg *sync.WaitGroup, tweetChan chan *Tweet) {
    defer wg.Done()
    for t := range tweetChan {
        if t.IsTalkingAboutGo() {
            fmt.Println(t.Username, "\ttweets about golang")
        } else {
            fmt.Println(t.Username, "\tdoes not tweet about golang")
        }
    }
}

func main() {
    start := time.Now()
    stream := GetMockStream()
    var wg sync.WaitGroup
    tweetChan := make(chan *Tweet)
    wg.Add(2)
    go producer(&wg, stream, tweetChan)
    go consumer(&wg, tweetChan)
    wg.Wait()
    fmt.Printf("Process took %s\n", time.Since(start))
}

到这里,我们也就讲完了《处理生产者和消费者代码中的死锁问题的方法》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

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