Golang’s method of achieving high concurrency: First, M is associated with a kernel thread, and through the scheduling of scheduler P, it connects to one or more G; then based on the one-to-one relationship between M and P, through P Schedule N Gs; finally realize the many-to-many relationship between kernel threads and Gs [M:N].
golang’s method of achieving high concurrency:
go language uses MPG mode to implement CSP
Video Course Recommendation →: "Concurrency Solution for Tens of Millions of Data (Theory and Practical Combat)"
has a lot to do with traditional concurrency Threads will only increase CPU and memory overhead. Too many threads will consume a large amount of computer hardware resources, causing a concurrency bottleneck.
M
refers to Machine. An M is directly associated with a kernel thread.
P
refers to "processor", which represents the context environment required by M and is also the processor that handles user-level code logic.
#G
refers to Goroutine, which is actually a lightweight thread in nature.
My personal understanding: M is associated with a kernel thread, and through the scheduling of scheduler P (context), one or more can be connected G is equivalent to dividing a kernel thread into N user threads. M and P have a one-to-one relationship (but the relationship changes in actual scheduling). N Gs are scheduled through P (P and G have a one-to-many relationship). ), realizing the many-to-many relationship (M:N) between the kernel thread and G. In this way, one kernel thread can start N Goroutines, and the number of user threads available for machines with the same hardware configuration will increase geometrically, and the concurrency will be greatly increased. improve.
Related learning recommendations: Go language tutorial
The above is the detailed content of How to achieve high concurrency in golang?. For more information, please follow other related articles on the PHP Chinese website!