package main
import (
"fmt"
"runtime"
)
func main() {
// This prints logical CPU usable by current process
s := runtime.NumCPU()
fmt.Println(s)
}
为什么runtime.NumCPU执行时输出4?
上述代码的输出:
4
Process finished with exit code 0
答案 0 :(得分:2)
这是当操作系统向处理器询问逻辑CPU数量时给出的编号。
对于Intel Core Processors,这是线程数。
例如:
8th Generation Intel® Core™ i5 Processors
Intel® Core™ i5-8250U Processor
核数4
线程数8
答案 1 :(得分:-1)
documentation(以及您的摘录中的评论)说:
NumCPU返回当前进程可用的逻辑 CPU的数量。
您可能对逻辑(相对于物理)CPU的含义感到困惑,并且期望看到“ 1”作为输出。现代的 CPU 具有多个内核,每个内核可以运行多个线程(通过称为“ 超线程”的技术)。如果有这些核心线程,则逻辑CPU的数量实际上是总数。在您的情况下,您可能有1个带有2个核心的CPU,每个核心运行2个线程,总共有4个逻辑CPU。