当前位置:网站首页>Go language concurrency model mpg model

Go language concurrency model mpg model

2022-06-24 14:21:00 Crying while learning

Go There are three important components in the language scheduler :M、P、G

  • M yes Machine, System threads . It is managed by the operating system ,goroutine Just run in M Above .M It's a big structure , It maintains small object memory cache(mcache)、 Currently executed goroutine、 Random number generator and so on .
  • P yes Processor, Represents the program execution context 、 processor .P It can be seen as a local scheduler ,P Will wait to run G And M Docking . It maintains a goroutine queue , namely runqueue.GO The runtime system will make M and P Build or disconnect in time .
  • G yes goroutine, There can be multiple . It contains the stack 、 Instruction pointer 、 Waiting place channel, And other effects on scheduling goroutine Important information .

MPG Model

Ongoing goroutine For blue , In a state of pending execution goroutine It's gray . Execu goroutine All stored in runqueue in ,P Maintainer this runqueue. Whenever by new goroutine Be performed , Will join in runqueue.

         Actually go Language runtime systems did not exist in the early days P The concept ,go The language scheduler directly puts G Assign to the appropriate M On . But the problem with this is G The concurrent runtime needs to apply for resources from the system , It will cause performance loss .

         from go Of 1.1 Version start , Add... To the runtime system P, from P To manage G object ,M Want to run G You need to bind P. The advantage of this is , Can be in P Object to apply for some system resources in advance , When G When you need it, ask your P To apply .P If resources are not enough , To apply for resources from the global , And will take some more resources for the later G To use the .

        and P Decoupled M and G The relationship between . If there is G There's a blockage ,P You can take other G With the new M Binding , bring G It always works in time , To improve concurrency .

goroutine happening

 M1 May be newly created , It may also be taken directly from the cache .

When G0 When blocking occurs ,P Will be with the current M0、G0 Unbundling .M1 And P Binding , Carry on P Others under goroutine.

runqueue Execution completed

When Processor Of runqueue Execution completed , It's empty time . He will learn from others Processor Steal half of the goroutine.

 

原网站

版权声明
本文为[Crying while learning]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241237270331.html