Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
运行goroutines直到其中一个得到有效结果_Go - Fatal编程技术网

运行goroutines直到其中一个得到有效结果

运行goroutines直到其中一个得到有效结果,go,Go,有没有办法运行一些goroutine,直到其中一个返回有效值(大于零的整数)? 在一个goroutine中,我要猜一个我需要放在数学公式中的数字你必须自己写这个 goroutine一直运行到它返回。每个goroutine自己决定何时返回 假设您有函数/过程A、B和C,每个函数/过程都进行长时间的计算,其中一个可能会首先找到有用的答案,如果是这样,其他两个应该停止。在这种情况下,您将需要派生三个进行计算的goroutine: func doA(args) { ... do computin

有没有办法运行一些goroutine,直到其中一个返回有效值(大于零的整数)?
在一个goroutine中,我要猜一个我需要放在数学公式中的数字

你必须自己写这个

goroutine一直运行到它返回。每个goroutine自己决定何时返回

假设您有函数/过程A、B和C,每个函数/过程都进行长时间的计算,其中一个可能会首先找到有用的答案,如果是这样,其他两个应该停止。在这种情况下,您将需要派生三个进行计算的goroutine:

func doA(args) {
    ... do computing for A ...
    ... deliver a result ...
    return  // this line is redundant, and here only for illustration
}

func doB(args) {
    ... do computing for B ...
    ... deliver a result ...
}
等等

参数中有什么内容?好吧,这取决于你,但最好给这三个函数提供某种方式,以发现其他函数中的一个已经给出了有用的答案,它们应该停止。一个相当聪明的方法是建立一个渠道,让那些认为结果“有用”的人关闭这个渠道,例如,表示其他人都应该停止工作。然后,所有
do
函数可以如下所示:

func doA(done chan struct{}, other_args) {
    var result_ready bool

    for !result_ready {
        select {
        case <-done: // someone else delivered a good result
            return   // so stop working now
        default:
            ... work a bit more ...
        }
    }
    ... deliver result ...
}
func doA(done chan struct{}, resultChan chan resulttype, args) {
    for {
        select {
        case <-done:
            return
        default:
             ... do a little work ...
             ... try to deliver result-so-far ...
        }
    }
}
select {
case <-done:
    return
case resultChan <- result:
}
到目前为止,尝试交付的结果应该是这样的:

func doA(done chan struct{}, other_args) {
    var result_ready bool

    for !result_ready {
        select {
        case <-done: // someone else delivered a good result
            return   // so stop working now
        default:
            ... work a bit more ...
        }
    }
    ... deliver result ...
}
func doA(done chan struct{}, resultChan chan resulttype, args) {
    for {
        select {
        case <-done:
            return
        default:
             ... do a little work ...
             ... try to deliver result-so-far ...
        }
    }
}
select {
case <-done:
    return
case resultChan <- result:
}
选择{

案例是的。您尝试过什么?您面临什么问题?只需启动多个goroutine,每个goroutine都应该提供结果在一个频道上。当你得到你想要的时,用一个频道或一个
context.context
@icza取消信号。如果我将向一个频道发送取消信号,只有一个goroutine会收到它,不是吗?关闭指定的频道,所有goroutine试图从中接收的都会“收到消息”。从封闭通道接收可以立即进行(产生通道元素类型的零值)。@icza谢谢,我收到了