首頁 > 後端開發 > Golang > 主體

透過通道和 goroutine 寫入切片:為什麼切片最終為空

王林
發布: 2024-02-11 18:50:08
轉載
1237 人瀏覽過

通过通道和 goroutine 写入切片:为什么切片最终为空

php小編魚仔為您揭示一個關於切片的問題:為什麼透過通道和 goroutine 寫入切片最終為空?在Go語言中,通道和 goroutine 是並發程式設計的重要工具,但在某些情況下,使用它們寫入切片可能會出現意外結果。本文將詳細解釋這個問題的原因,並提供解決方案,幫助您更好地理解和處理這種情況。

問題內容

我執行這個函數:

func run() () {
    // this slice is going to be filled out by a channel and goroutine.
    vertices := make([]vertex, 0)

    var wg sync.waitgroup

    // obtain a writer to fill out the vertices.
    writer := writer(&wg, vertices)

    // run an arbitrary logic to send data to writer.
    logic(writer)

    // stop the writer reading on the channel.
    close(writer)

    // wait for the write to complete.
    wg.wait()

    // see if vertices slice is actually filled out.
    doublecheckvertices(vertices)
}
登入後複製

但最終,我的 vertices 切片是空的:

func doublecheckvertices(vertices []vertex) () {
    // here i notice that `vertices` slice is actually empty :(

}
登入後複製

傳回 writer 的函數是這樣的:

func writer(wg *sync.waitgroup, vertices []vertex) (chan<- []*triangle3) {
    // external code writes to this channel.
    // this goroutine reads the channel and writes to vertices.
    writer := make(chan []*triangle3)

    // write by a goroutine.
    wg.add(1)
    go func() {
        defer wg.done()

        a := vertex{}

        // read from the channel and write them to vertices.
        for ts := range writer {
            for _, t := range ts {
                a.x = float32(t.v[0].x)
                a.y = float32(t.v[0].y)
                a.z = float32(t.v[0].z)
                vertices = append(vertices, a)
            }
        }
    }()

    return writer
}
登入後複製

任何人都可以幫我弄清楚為什麼我的 vertices 切片最終是空的嗎?

紀錄

日誌顯示 vertices 切片實際上已填入。但由於某種原因,傳遞給doublecheckvertices時為空。

                vertices = append(vertices, a)
                // This Log shows the slice is actually filled out:
                fmt.Printf("vertices len() is %v\n", len(vertices))
登入後複製

解決方法

這看起來類似於「將切片作為函數參數傳遞,並修改原始切片

如果您希望 goroutine 修改您在外部建立的切片,則需要一個指向該切片的指標:

func Writer(wg *sync.WaitGroup, vertices *[]Vertex) (chan<- []*Triangle3) {
    // External code writes to this channel.
    // This goroutine reads the channel and writes to vertices.
    writer := make(chan []*Triangle3)

    // Write by a goroutine.
    wg.Add(1)
    go func() {
        defer wg.Done()

        a := Vertex{}

        // Read from the channel and write them to vertices.
        for ts := range writer {
            for _, t := range ts {
                a.X = float32(t.V[0].X)
                a.Y = float32(t.V[0].Y)
                a.Z = float32(t.V[0].Z)
                *vertices = append(*vertices, a)  <=====
            }
        }
    }()

    return writer
}
登入後複製

以上是透過通道和 goroutine 寫入切片:為什麼切片最終為空的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:stackoverflow.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!