Home>Article>Backend Development> golang concurrency is not parallelism

golang concurrency is not parallelism

(*-*)浩
(*-*)浩 Original
2019-12-17 11:52:22 2111browse

golang concurrency is not parallelism

# The core developer of Rob Pike, the core developer of it specifically mentioned this topic (Recommended Learning:go
)

Although we use go in the for loop to create a goroutine, we take it for granted that every time we loop through variables, golang will definitely execute this goroutine and then output the variables at that time.At this time, we fell into a fixed mindset. The default concurrency equals parallelism.

It is true that the goroutine created by go will execute the function code concurrently.

But will it be executed every time we loop as we imagined?

the answer is negative!

Rob Pike specifically mentioned that concurrency in golang refers to the fact that certain functions in the code structure can logically run at the same time, but physically they may not run at the same time. Parallelism refers to the use of different CPUs to perform different or the same tasks at the physical level.

Golang's goroutine scheduling model determines that each goroutine runs in a virtual CPU (that is, the number of virtual CPUs we set through runtime.GOMAXPROCS(1)).The number of virtual CPUs may not match the actual number of CPUs. Each goroutine will be selected and maintained by a specific P (virtual CPU), and M (physical computing resource) will select a valid P each time, and then execute the goroutine in P.

Each P will put the goroutine it maintains into a G queue, which includes goroutine stack information, executable information, etc. By default, the number of P's is equal to the number of actual physical CPUs.

So when we create goroutines through loops, each goroutine will be assigned to a different P queue. The number of M is not unique. When M randomly selects P, it is equivalent to randomly selecting a goroutine.

In this question, we set P=1. So all goroutines will be bound to the same P. If we modify the value of runtime.GOMAXPROCS, we will see another order. If we output the goroutine id, we can see the effect of random selection:

func main() { wg := sync.WaitGroup{} wg.Add(20) for i := 0; i < 10; i++ { go func() { var buf [64]byte n := runtime.Stack(buf[:], false) idField := strings.Fields(strings.TrimPrefix(string(buf[:n]), "goroutine "))[0] id, err := strconv.Atoi(idField) if err != nil { panic(fmt.Sprintf("cannot get goroutine id: %v", err)) } fmt.Println("go routine 1 i: ", i, id) wg.Done() }() } for i := 0; i < 10; i++ { go func(i int) { var buf [64]byte n := runtime.Stack(buf[:], false) idField := strings.Fields(strings.TrimPrefix(string(buf[:n]), "goroutine "))[0] id, err := strconv.Atoi(idField) if err != nil { panic(fmt.Sprintf("cannot get goroutine id: %v", err)) } fmt.Println("go routine 2 i: ", i, id) wg.Done() }(i) } wg.Wait() }

The output is as follows:

go routine 2 i: 9 24 go routine 1 i: 10 11 go routine 1 i: 10 5 go routine 1 i: 10 6 go routine 2 i: 3 18 go routine 1 i: 10 9 go routine 1 i: 10 10 go routine 1 i: 10 8 go routine 2 i: 0 15 go routine 2 i: 4 19 go routine 2 i: 6 21 go routine 1 i: 10 7 go routine 1 i: 10 14 go routine 2 i: 7 22 go routine 2 i: 8 23 go routine 1 i: 10 13 go routine 2 i: 5 20 go routine 1 i: 10 12 go routine 2 i: 1 16 go routine 2 i: 2 17 ⋊> ~/S/g/g/s/t/C/goroutine ./goroutine go routine 1 i: 10 11 go routine 2 i: 9 24 go routine 1 i: 10 6 go routine 1 i: 10 14 go routine 1 i: 10 9 go routine 1 i: 10 10 go routine 1 i: 10 12 go routine 2 i: 0 15 go routine 1 i: 10 13 go routine 1 i: 10 5 go routine 2 i: 1 16 go routine 2 i: 5 20 go routine 1 i: 10 7 go routine 2 i: 7 22 go routine 2 i: 3 18 go routine 2 i: 2 17 go routine 2 i: 4 19 go routine 1 i: 10 8 go routine 2 i: 8 23 go routine 2 i: 6 21

We return to this question again, although in the loop A goroutine is defined through go. But as we said, concurrency does not mean parallelism. Therefore, although it has been defined, it may not be implemented at this time.You need to wait for M to select P before executing the goroutine. Regarding how goroutine is scheduled in golang (GPM model), you can refer to Scalable Go Scheduler Design Doc or LearnConcurrency

The above is the detailed content of golang concurrency is not parallelism. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn