Golang 需要至少 5 个操作系统线程

Golang 需要至少 5 个操作系统线程

  • 主线程:Golang 代码执行的入口点,负责初始化程序,并启动其他 Goroutine。
  • 垃圾回收器线程:Golang 内置了垃圾回收器,使用专门的线程来执行垃圾回收操作,回收不再使用的内存空间。
  • CPU 核心数个系统线程:每个核心需要一个系统线程来支持并发任务的执行。
  • 系统监视线程:用于监视所有线程状态,包括 Goroutine 的创建和销毁,防止出现死锁和其他问题。

*. 因此,在常规情况下,Golang 所需的最小线程数为 5。这些线程共同协作,支持 Golang 运行时的正常运行和 Goroutine 的并行执行。

代码验证

package main

import (
	"fmt"
	"runtime"
	"runtime/pprof"
)

func main() {

	runtime.GOMAXPROCS(1)
	threadProfile := pprof.Lookup("threadcreate")
	fmt.Printf(" init threads counts: %d\n", threadProfile.Count())
	fmt.Printf(" end threads counts: %d\n", threadProfile.Count())

}
posted @ 2023-04-02 12:52  vx_guanchaoguo0  阅读(87)  评论(0编辑  收藏  举报