On the goroutine of Go language concurrency mechanism

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

One of the big advantages of the go language is that it is easy to write concurrent programs. The goroutine mechanism is built into the go language. This is something like Coroutaine (co-process). But it's not exactly the same.

Like this example:

package mainimport ("fmt""strconv")func main() {ch := make(chan int)task("A", ch)task("B", ch)fmt.Printf("begin\n")<-ch<-ch}func task(name string, ch chan int) {go func() {i := 1for {fmt.Printf("%s %d\n", name, i)i++}ch <- 1}()}

After running, it is found that a B two goroutine will be executed alternately, not as the traditional process requires manual schedule. It looks amazing.

Change the code a little bit and put

fmt.Printf("%s %d\n", name, i)

Change into

print(name + " " + strconv.Itoa(i) + "\n")

Look again. The magical effect disappears and only a is run.

So FMT. What is the difference between Printf and print, which leads to this result?

Roughly turned the code of Go, see the go language in C Lib packaging on the use of a CGO way. When the C library is called through CGO, it is automatically schedule when called, and then returns at the end of the call. The difference between the two results lies in the FMT. PRINTF is packaged by CGO, while print is native. So the FMT is called. In Printf, scheduling is automatically implemented.

Traditional Coroutaine can still block and lose concurrency when accessing network, database and other IO operations, so it usually needs to be implemented in conjunction with asynchronous IO. The existing libraries, if they do not provide asynchronous functionality, are difficult to do and often need to be re-implemented. Furthermore, even with asynchronous IO functionality, additional development is required in order to behave in the same way as in previous sequential programs.

This implementation of the go language is a good solution to this problem, can make full use of the existing large number of C library to package.

At the same time, you can still use runtime. Gosched () to manually dispatch. It is also necessary to be in a scenario with a large computational capacity.

In the example of using print, if runtime is used. Gomaxprocs (2), you can re-parallel up again. At this point, two goroutine are running in two separate threads. This is another point of Goroutine and co-process. However, enabling multiple threads does not necessarily make the program faster. The cost of context switching across threads is also significant. In the simple example above, it also makes the program slower. Reducing the context switching overhead is also an advantage of the co-process.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.