第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何解決生產(chǎn)者和消費者代碼中的死鎖

如何解決生產(chǎn)者和消費者代碼中的死鎖

Go
呼啦一陣風(fēng) 2023-02-14 17:55:20
當(dāng)我運行下面的程序時,出現(xiàn)錯誤davecheney      tweets about golangbeertocode      does not tweet about golangironzeb         tweets about golangbeertocode      tweets about golangvampirewalk666  tweets about golangfatal error: all goroutines are asleep - deadlock!goroutine 1 [semacquire]:sync.runtime_Semacquire(0xc000010260?)        /usr/local/go/src/runtime/sema.go:56 +0x25sync.(*WaitGroup).Wait(0x100c000058058?)        /usr/local/go/src/sync/waitgroup.go:136 +0x52main.main()        /home/joe/go/src/github.com/go-concurrency-exercises/1-producer-consumer/main.go:53 +0x14f死鎖從何而來,如何改進程序以避免死鎖?package mainimport (    "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的并發(fā)版本
查看完整描述

1 回答

?
墨色風(fēng)雨

TA貢獻1853條經(jīng)驗 獲得超6個贊

對 wg.Wait() 的調(diào)用一直在等待,直到組的計數(shù)器為零,但是沒有正在運行的 goroutines 來遞減計數(shù)器。


通過在從 goroutine 函數(shù)返回之前調(diào)用 wg.Done() 來修復(fù):


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

}


查看完整回答
反對 回復(fù) 2023-02-14
  • 1 回答
  • 0 關(guān)注
  • 133 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號