go language goroutine

Go language goroutine

Want in other languages ​​multitasking in a program, such as python, python multi-task can use multi-process, multi-threaded, Ctrip. But more resource-intensive process, multi-threaded multi-core can not play advantage (GIL), python coroutine is single-threaded, you must make concessions such as a task, another task to perform, if one task blocking live, let no cpu come, then the entire program will be blocked live.

goroutine (coroutines) go language is a concept similar to thread, but it is more than a lightweight thread. When enabling a plurality goroutine, each program will goroutine assigned to each of the cpu core, we can give full play cpu multicore efficiency. go coroutine efficiency ratio m (cpu Audit) / n (goroutine number), while the efficiency of the python coroutine ratio of 1 / n

Open goroutine

Only need to be packaged into a task function, go use keywords, you can open a goroutinecomplete multitasking. Meanwhile, the main function of this program is agoroutine

package main

import "fmt"

func work() {
	fmt.Printf("work goroutine")
}

func main() {
	go work()
	fmt.Println("main goroutine")
}

sync.WaitGroup

The above program starts goroutine is no problem, but after running the compiler will find only printed main goroutine. This is because, after starting to open another goroutine in the main goroutine inside, the other open goroutine not had time to run, the main goroutine has ended, the end of the main goroutine, opened in the main goroutine goroutine will end all, think of all the main goroutine to perform other goroutine, you have to wait.

package main

import (
	"fmt"
	"time"
)

func work() {
	fmt.Println("work")
}

func main() {
	go work()
	fmt.Println("main goroutine")
	time.Sleep(time.Second)
}

Use time.Sleep to sleep is a program to let the other goroutine achieve a sufficient time to run
but when opened more goroutine, we do not know all of the coroutine has finished running, how much time we can not estimate the time of sleep. sleep time may be more, maybe less, less will make you want to be able to reach the objective, the program will allow more and more sleep, a waste of resources, at this time, we need to introduce sync tool to solve this module problem

package main

import (
	"fmt"
	"sync"
)

var wg sync.WaitGroup

func work() {
	defer wg.Done()
	fmt.Println("work")

}

func main() {
	wg.Add(1)
	go work()
	fmt.Println("main goroutine")
	wg.Wait() // 等待所有的协程能全部运行完毕
}

Use sync.WaitGroup, the current need to open a coroutine, to WaitGroupwhich was added 1, after the coroutine to WaitGroupinform has been completed (may be blended defer Off shear words used), and finally the last used primary goroutine (main function) WaitGroupto wait for all goroutine end of run (corresponding to the function execution end)

Goroutine relations and threads

Thread here refers to the operating system threads, threads and goroutine starting occupied resources are not the same, the thread's stack memory is generally 2mb, but goroutine at the beginning of the occupation of the stack memory is 2kb, goroutine stack is not fixed, it expansion can go like slices, like, up to a maximum limit of G, all can be easily opened in one hundred thousand goroutine go inside.

  • GMP scheduling
    • G: G goroutine representatives, and binding information stored inside the current goroutine P contained information such as current goroutine
    • M: M is go running virtual operating system threads. A goroutine will eventually need to run on M
    • P: P goroutine queue managers a set, P which kept the current context of goroutine, P will goroutine current management of the queue to do some scheduling (goroutine may take some cpu time is too long, P goroutine this will pause, and then to let other goroutine to perform). When his goroutine run all over, the P will queue to go inside to take a global, overall if there is not, it will go to other P even get inside to take goroutine, this can be achieved to maximize the efficiency of
    • P and M are generally one to one. Their relationship is: P manages a group G mount to run on M. When a long block on a G M, runtime creates a new M, P will blocking G G where other mount the new M. When the old G or complete blockage that has recovered its old die M.

Guess you like

Origin www.cnblogs.com/ivy-blogs/p/12659128.html