go - 我怎样才能生成依赖于它们的前辈的例程?

标签 go concurrency goroutine

例如,我想填充这个矩阵:

| 0 | 0 | 0 | 0 |
| 0 | 1 | 2 | 3 |
| 0 | 2 | 3 | 4 |
| 0 | 3 | 4 | 5 |

具体来说,我想填充它以便每个单元格都遵循规则,

formula

在英语中,一个单元格的值比它的 toplefttopleft 邻居值的最大值大 1。

因为每个单元格只有三个依赖项(它的 toplefttopleft 邻居),我可以在 处填充单元格>m[1][1](1),填充后,我可以填充标记为 2 的单元格,因为它们的所有依赖项都已经存在人口稠密。

| 0 | 0 | 0 | 0 |          | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 0 |   ---\   | 0 | 1 | 2 | 0 |
| 0 | 0 | 0 | 0 |   ---/   | 0 | 2 | 0 | 0 |
| 0 | 0 | 0 | 0 |          | 0 | 0 | 0 | 0 |

我如何启动 1 个 go routine,然后是 2,然后是 3,然后是 4...,以填充该矩阵的每个对角线?也许更具体地说,我如何才能在开始依赖单元格之前等待邻居完成?

[编辑]:感谢@Zippoxer 的评论!为了澄清,我问的是 Go 中的语法是什么来运行一个依赖于另一个先完成的 go-routine。因为当一个 go-routine 完成时可以启动多个新的 go-routines,所以它不像没有并发地调用事物那么简单!

最佳答案

希望这是一个人为的例子,因为这绝对不是最快的解决方案,但也许这就是您想要的。

每个cell都运行在自己的goroutine中,有自己的channel,大致代表了它的依赖关系。一旦从其 channel 读取三个值,单元就会知道其依赖关系已全部解决。当一个单元格完成时,它会将一些值传递给其所有依赖项的 channel 。

import "sync"

type empty struct{}

func contrivedMathThing(i, j int) ([][]int) {

    var wg sync.WaitGroup
    wg.Add(i * j)

    // Make two-dimensional slices for the channels that the goroutines will
    // wait on, and for the results of each cell. Two dimensional slices are
    // more annoying to make, but I think make the example a bit more clear.
    chans := make([][]chan empty, i)
    results := make([][]int, i)
    for a := 0; a < i; a++ {
        chans[a] = make([]chan empty, j)
        results[a] = make([]int, j)
        for b := 0; b < j; b++ {
            chans[a][b] = make(chan empty, 3)
        }
    }

    for a := 0; a < i; a++ {
        for b := 0; b < j; b++ {
            go func(a, b int, waitc <-chan empty) {
                defer wg.Done()

                // Wait for all dependencies to complete
                <-waitc
                <-waitc
                <-waitc

                // Compute the result
                // Too lazy to write...

                // Save the result to the results array
                results[a][b] = result

                // Signal all dependents that one of their dependencies has
                // resolved
                if a < i - 1 {
                    chans[a + 1][b] <- empty{} 
                }
                if b < j - 1 {
                    chans[a][b + 1] <- empty{}
                }
                if a < i - 1 && b < j - 1 {
                    chans[a + 1][b + 1] <- empty{}
                }

            }(a, b, chans[a][b])
        }
    }

    // All cells in the first row and first column need to start out with two
    // of their dependencies satisfied.
    for a := 1; a < i; a++ {
        chans[a][0] <- empty{}
        chans[a][0] <- empty{}
    }
    for b := 1; b < j; b++ {
        chans[0][b] <- empty{}
        chans[0][b] <- empty{}
    }

    // Unblock the cell at [0][0] so this show can get started
    close(chans[0][0])

    wg.Wait()

    return results
}

关于go - 我怎样才能生成依赖于它们的前辈的例程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34323986/

相关文章:

ubuntu - 如何在 Ubuntu Precise 中安装当前版本的 Go

go - 被嵌入在golang结构中的接口(interface)所困惑

java - 未知的并发修改异常java使用Iterator和Vector

java - 这个多线程代码保证总是返回零吗?

go - Golang中“append”的最大O是什么?

java - Java 中的 JPanel 发生了什么?我究竟做错了什么?

go - 有没有一种方法可以在一个 goroutine 返回后延迟一段时间后取消上下文?

pointers - 如何在 golang 中创建真正的比赛条件

go - 第一个完成时如何安全地绕过其他 goroutine 的结果

string - 查找字符串中子字符串的索引,指定起始索引