Concurrency – can I receive results from one of multiple goroutines in go?

I just learned about Google's programming language go I'm interested in the concurrency support it provides and set out to learn more about it However, I went to see how go implements the specific function of concurrency. So far, I haven't seen any evidence that this function fully exists

This is a hypothetical case: suppose we are programming a function to determine the foo value of a specific input For any given input, the foo value can be found in domain a or domain B (not both) The search techniques in these domains are completely different, but they share the attributes that successful searches tend to return quickly, while unsuccessful searches must traverse the entire data set to be exhaustive, so it takes a long time

Now, in other languages that use concurrency (such as cilk), the foosearch function can be programmed so that it generates the asearch function and the bsearch function These functions will run at the same time, and whenever any one of them gets an answer, the answer will be reported to the calling function foosearch, which will terminate any functions it has generated but has not returned

However, with go's goroutines, it seems that you can only connect two routines through one channel - so you can't set the channel that asearch or bsearch can send, depending on finding the answer first and reading foosearch from it It looks like you can't read from the channel without blocking - so you can't let foosearch start asearch and bsearch, set the channel from both, and then run a check in the loop to see if one or which produces an answer

Is my understanding of the concurrency limit of go correct? Is there another way to achieve a given result?

Solution

No, I don't believe your understanding of the go limit is correct

First, I don't see any channels in go that restrict communication between two routines You can pass the same channel to asearch and bsearch, and then any completed channel can send results on this channel

If you want to use two channels and wait for one to get the result, you can use the select statement Starting with go tutorial, an example of selecting a channel to send a request and an example of notifying the server to exit:

21    func server(op binOp,service chan *request,quit chan bool) {
22        for {
23            select {
24            case req := <-service:
25                go run(op,req);  // don't wait for it
26            case <-quit:
27                return;
28            }
29        }
30    }

In addition, although receiving from the channel is usually blocked, you can also perform non blocking receive from the channel

Therefore, there are several ways to wait for the results of multiple goroutines without blocking I think I will choose to use multiple channels of select multiplexing, because you can easily tell which routine returns the result without packaging this information into the value you send or other forms of out of band communication

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>