Home > Backend Development > Golang > When Does the Go Scheduler Create New M and P?

When Does the Go Scheduler Create New M and P?

Mary-Kate Olsen
Release: 2024-10-29 18:39:43
Original
353 people have browsed it

When Does the Go Scheduler Create New M and P?

When Go Scheduler Creates New M and P

In the Go runtime, goroutines, operating system threads (OS threads), and contexts/processors (M and P) cooperate to manage concurrency. Understanding when new M and P entities are created is crucial for efficient performance.

M and P Creation

  • M (Machine): An M represents an OS thread and is created when a goroutine is scheduled to run. The number of M is initially set by the runtime.GOMAXPROCS environment variable.
  • P (Processor): A P is a processor that executes goroutines. Each P is associated with an M. When an M blocks, a new M may be created to execute goroutines on that P.

Scenario Analysis

In your code example, you create multiple goroutines that perform blocking operations. In this scenario:

  1. The first batch of goroutines will be scheduled on the existing P instances.
  2. If all P's local queues are full, new M will be created to handle the overflow goroutines.
  3. However, since your goroutines perform blocking operations within the go func(), the M associated with blocked goroutines will be removed from the P and put on the idle thread pool.
  4. New M will be created to replace the blocked M and continue executing goroutines on that P.

Conclusion

In summary, the Go scheduler creates a new M when a goroutine is scheduled to run and a free OS thread is unavailable. A new P is created when the local queue of an existing P is full. However, in the case of blocking goroutines, the number of M created can exceed the number of virtual cores, as each blocking operation requires a separate M.

Additional Resources

  • [GOMAXPROCS in the Go Blog](https://blog.golang.org/GOMAXPROCS)
  • [Go Goroutine, OS Thread, and CPU Management](https://godoc.org/runtime/debug#SetMaxThreads)
  • [GMP Basics](https://www.programmersought.com/article/79557885527/)

The above is the detailed content of When Does the Go Scheduler Create New M and P?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template