go-goroutine coroutine and multi-core scheduling


title: go-goroutine coroutine and multi-core scheduling
categories: Go
tags: [go, goroutine, coroutine, scheduling]
date: 2020-11-21 14:25:12
comments: false
mathjax: true
toc: true

go-goroutine coroutine and multi-core scheduling


Prequel

  • Ten Thousand-Word Long Articles Explain the Evolution, Scheduling, Memory and Practice of Golang Runtime in a Simple Way-https://www.toutiao.com/i6855166544079487492/
  • Golang's coroutine scheduler principle and GMP design ideas? -https://www.toutiao.com/i6797580089891488259/

go func() scheduling process


golang multi-core scheduling and goroutine coroutine

reference:

  • A little understanding of golang multi-core programming-https://studygolang.com/articles/9686
  • Multi-core scheduling in Go language-https://blog.csdn.net/InsZVA/article/details/54081605
  • Golang's coroutine scheduling mechanism and GOMAXPROCS performance tuning-https://juejin.im/post/5b7678f451882533110e8948
  • Golang's coroutine scheduler principle and GMP design idea-https://www.toutiao.com/i6797580089891488259/

GOMAXPROCS

In the Go language runtime (runtime) implements a small task scheduler. The working principle of this scheduler is similar to the operating system scheduling thread. The Go program scheduler can efficiently allocate CPU resources to each task. In traditional logic, developers need to maintain the corresponding relationship between the number of threads in the thread pool and the number of CPU cores. Similarly, Go can also be done in the runtime.GOMAXPROCS() function, the format is:

runtime.GOMAXPROCS (number of logical CPUs)

The number of logical CPUs here can have the following values:

  • <1: Do not modify any value.
  • =1: Single core execution.
  • >1: Multi-core concurrent execution.

In general, you can use runtime.NumCPU() to query the number of CPUs, and use the runtime.GOMAXPROCS() function to set, for example:

runtime.GOMAXPROCS(runtime.NumCPU())

Before Go 1.5, single-core execution was used by default. Starting from Go 1.5, the above statement is executed by default in order to allow the code to be executed concurrently and to make the most efficient use of the CPU.

GOMAXPROCS is also an environment variable. Setting the environment variable before the application starts can also play the same role.


seize

  • Preemption: In coroutine, it is necessary to wait for a coroutine to give up the CPU before executing the next coroutine. In Go, a goroutine occupies up to 10ms of CPU to prevent other goroutines from starving. This is the difference between goroutine and coroutine. -https://www.toutiao.com/i6797580089891488259/

goroutine coroutine

Relevant information
  • Ten Thousand-Word Long Articles Explain the Evolution, Scheduling, Memory and Practice of Golang Runtime in a Simple Way-https://www.toutiao.com/i6855166544079487492/

Switch coroutine
  • The coroutine in the user code understands the blocking caused by only switching the coroutine without blocking the thread.-https://www.toutiao.com/i6855166544079487492/

Guess you like

Origin blog.csdn.net/yangxuan0261/article/details/110238712