在goroutines之间共享数组的问题

发布于 2025-01-26 16:26:52 字数 1569 浏览 1 评论 0原文

我正在尝试解决此Golang练习 https:/// github.com/loong/go-concurrency-eccises/tree/master/1-producer-consumer

我想我接近解决方案,但是我遇到了僵局错误,

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!

这是我

func producer(stream Stream) (tweets []*Tweet) {
    for {
        tweet, err := stream.Next()
        if err == ErrEOF {
            return tweets
        }

        tweets = append(tweets, tweet)
    }
}

func consumer(tweets []*Tweet) {
    for _, t := range tweets {
            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()

    data := make(chan []*Tweet)
    var wg sync.WaitGroup

    wg.Add(3)
    // Producer
    go func() {
        tweets := producer(stream)
        data <- tweets
    }()



    // Consumer
    go func() {
        defer wg.Done()
        tweets := <-data
        consumer(tweets)
    }()

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

解决方案失败的代码?

提前致谢

I am trying to solve this golang exercise https://github.com/loong/go-concurrency-exercises/tree/master/1-producer-consumer.

I guess I am close to the solution but I am getting a deadlock error

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!

here is my code

func producer(stream Stream) (tweets []*Tweet) {
    for {
        tweet, err := stream.Next()
        if err == ErrEOF {
            return tweets
        }

        tweets = append(tweets, tweet)
    }
}

func consumer(tweets []*Tweet) {
    for _, t := range tweets {
            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()

    data := make(chan []*Tweet)
    var wg sync.WaitGroup

    wg.Add(3)
    // Producer
    go func() {
        tweets := producer(stream)
        data <- tweets
    }()



    // Consumer
    go func() {
        defer wg.Done()
        tweets := <-data
        consumer(tweets)
    }()

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

Where y solution is failing?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断舍离 2025-02-02 16:26:52

发生僵局是因为您将3传递给wg.add(3),这意味着创建3个等待组,但您只需要1。

解决方案是替换wg.add( 3)带有wg.add(1)

检查我的演示代码在这里

Deadlock happened because you pass 3 to wg.Add(3), which means creating 3 waiting groups but you only need 1.

Solution is replacing wg.Add(3) with wg.Add(1).

Check my demo code here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文