卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章64334本站已运行4115

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

通过通道和 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 %vn", 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
}
卓越飞翔博客
上一篇: 为什么go无法访问docker中的8080端口
下一篇: 我是否需要额外往返 firestore 来读取创建和更新的时间戳字段?
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏