This section source location github.com/golang-mini…

In the past, we wrote concurrent programs to achieve the general use of multiple threads, maintain a thread pool, create, destroy, allocate resources at the appropriate time.

Go gives us a language-level support for concurrency, and Goroutine and Chan work together to give him an innate advantage.

The concept of Goroutine is similar to thread. When the Go program runs, it is automatically scheduled and managed. The system can intelligently allocate the tasks in Goroutine to THE CPU, so that these tasks can run concurrently as much as possible.

He contrasts with threads

In terms of use

  • Lighter than threads, can create hundreds of thousands, millions without worrying about resources.
  • andchanWith the use of high concurrency,goroutineMore convenient data transfer between.
  • If accessing the same data block, be careful with data race issues, shared lock versus mutex selection, and data synchronization for concurrent operations (described below).

In terms of its implementation

  • In terms of resources, the stack memory size of threads is usually fixed2MB, although this value can be set, but too large waste, too small is easy to insufficient, andgoroutineStack memory is variable and generally starts with2KB, with the demand can be expanded to 1GB. sogoroutineVery lightweight, and can meet different needs.
  • From the scheduling point of view, the scheduling of threads byOSKernel completion; Thread switching is requiredCPU registersData exchange in memoryTo switch different thread contexts. The triggering mode isCPU clockAnd thegoroutineIs more lightweight scheduling, by its own scheduler to complete.
  • The relationship between a coroutine and a thread is somewhat similar to the relationship between a line and a process.

Creation and Use

To create a Goroutine, just add the go keyword before the function.

goFunction name (parameter)Copy the code

See a dome

func quickFun(a){
	fmt.Println("maybe you can's see me!")}func main(a){
	go quickFun() // Create a goroutine
	fmt.Println("hey")
	time.Sleep(time.Second)
}
Copy the code
  • goroutinemainThe main thread runs at the same time
  • mainAt the end of the run, all coroutines are violently terminated, so the above program waited 1 second longer
  • GoThe program frommainThe packagemain()Function starts, and when the program starts,GoThe program will bemain()Function to create a defaultgoroutine

The output

hey
maybe you can's see me!
Copy the code

Yes, it’s that simple. If your function is only used here, you can also create a Goroutine with an anonymous function.

func main(a){
	go func(a) {
		fmt.Println("hello ")
	}()
	time.Sleep(time.Second) // When main finishes, all coroutines are violently terminated, so wait 1 second
}
Copy the code

PS: Unlike threads, goroutines don’t have unique ids, so we can’t operate on a particular coroutine.

goroutine

Goroutine is at the heart of parallel design for the Go language. A Goroutine is a much lighter implementation than threads, and a dozen goroutines can be just a few threads underneath. In fact, Go encapsulates and handles Goroutine scheduling in multiple aspects, such as runtime, system call, etc.

Using Goroutine is as simple as adding the go keyword in front of the function that needs to be executed. When goroutine is executed, Go immediately returns and executes the rest of the code without blocking the main thread.

Here’s a little code to illustrate the use of GO:

// First we implement an Add() function
func Add(a, b int) {
c := a + b
fmt.Println(c)
}

go Add(1.2) // Use the go keyword to execute the function concurrently
Copy the code

Concurrent execution of Go is as simple as that. When a function is preceded by the Go keyword, the function is executed concurrently in a new Goroutine. When the function is finished, the new Goroutine is terminated. Note, however, that if the function has a return value, the return value is discarded. So when to use go is a matter of discretion.

Let’s take a look at an example of what concurrency in Go looks like. Create a new source file, goroutine2.go, and enter the following code:

package main

import "fmt"

func Add(a, b int) {
	c := a + b
	fmt.Println(c)
}

func main(a) {
	for i := 0; i < 10; i++ {
		go Add(i, i)
	}
}
Copy the code

Why is it that executing the goroutine.go file shows nothing on the screen, but the program does not report an error?

The original 10 goroutines were started when the main program went through the for loop, and then the main program quit. The 10 goroutines were started before the Add() function was executed, so there was no output from the program. This means that the main Goroutine does not wait for other Goroutines to finish executing.

The Go language provides channels that specifically address the problem of concurrent communication, which we’ll cover in detail in the next section.

summary

Learning go requires learning concurrency. Through this section, we know that coroutines are very easy to create, and they are very lightweight and only occupy 2~ 4K. Other languages are easy to use M

The use of coroutines also has to do with data transfer, the producer-consumer model, and we’ll talk more about scheduling coroutines later.

In addition, the location and solution of concurrent bugs is a big problem, usually should pay attention to good code style and programming habits.