Home Backend Development Golang Is golang a multi-threaded mode?

Is golang a multi-threaded mode?

Jul 22, 2020 am 09:31 AM
golang Multithreading

Golang is a multi-threaded model. The thread model of golang is the MPG model. Generally speaking, Go processes and kernel threads correspond to many-to-many, so first of all, they must be multi-threaded.

Is golang a multi-threaded mode?

golang is a multi-threaded mode.

Since p and m in gmp bind p to the m kernel thread, then the maximum number of p is determined by GOPROCESS, and the number of M kernel threads will be limited to 10K by go. , but due to kernel reasons it cannot do so much, so this restriction can be treated as if it does not exist. Take a picture to clarify

Is golang a multi-threaded mode?

Golang has some so-called M ratio N model. N go routines can be created under M threads. Generally speaking, N is much larger than M. Essentially It is a multi-threaded model, but the scheduling of coroutines is determined by Go's runtime, emphasizing that developers should use channels for synchronization between coroutines.

As for threads, since the language level is not open, you can understand it as a multi-coroutine model. Several go routine can be created on one thread. Generally speaking, it will be created with the CPU The number of threads with the same number of cores is, of course, actually determined by the runtime.

Regarding the scheduling of goroutine, this is actually constantly evolving. I will only talk about the GMP model. Goroutine runs on M threads, and the current P will be checked every time a task is executed. The executable queue on the (processor) contains executable goroutines. Once there is no executable goroutine on the current P, it will steal the goroutine in the executable queue of another P.

Theoretically, the creation of goroutine is only limited by memory. Generally speaking, the maximum is 2KB. For a thread with 2MB space, theoretically, it can easily reach 1,000. Of course, this is only an ideal situation, so the OS The number of threads will not increase as the number of goroutines created increases. Thread scheduling is relatively performance-intensive for Go. Frequent switching of schedules will only exist between goroutines, and threads will only maintain the same number of active threads as the number of CPUs.

Related learning recommendations: Go language tutorial

The above is the detailed content of Is golang a multi-threaded mode?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to safely read and write files using Golang? How to safely read and write files using Golang? Jun 06, 2024 pm 05:14 PM

How to safely read and write files using Golang?

How to configure connection pool for Golang database connection? How to configure connection pool for Golang database connection? Jun 06, 2024 am 11:21 AM

How to configure connection pool for Golang database connection?

Similarities and Differences between Golang and C++ Similarities and Differences between Golang and C++ Jun 05, 2024 pm 06:12 PM

Similarities and Differences between Golang and C++

How steep is the learning curve of golang framework architecture? How steep is the learning curve of golang framework architecture? Jun 05, 2024 pm 06:59 PM

How steep is the learning curve of golang framework architecture?

Comparison of advantages and disadvantages of golang framework Comparison of advantages and disadvantages of golang framework Jun 05, 2024 pm 09:32 PM

Comparison of advantages and disadvantages of golang framework

What are the best practices for error handling in Golang framework? What are the best practices for error handling in Golang framework? Jun 05, 2024 pm 10:39 PM

What are the best practices for error handling in Golang framework?

golang framework document usage instructions golang framework document usage instructions Jun 05, 2024 pm 06:04 PM

golang framework document usage instructions

Golang framework source code performance tuning Golang framework source code performance tuning Jun 05, 2024 pm 04:39 PM

Golang framework source code performance tuning

See all articles