简体   繁体   中英

Running GO runtime.GOMAXPROCS(4) on a dual core cpu

Sorry, if this sounds stupid.

What will happen if run runtime.GOMAXPROCS(4) while runtime.NumCpu() == 2

runtime.GOMAXPROCS controls how many operating system-level threads will be created to run goroutines of your program (and the runtime powering it). (The runtime itself will create several more threads for itself but this is beside the point.)

Basically, that is all that will happen.

But supposedly, you intended to actually ask something like "how would that affect the performance of my program?", right? If yes, the answer is "it depends". I'm not sure whether you had a chance to work with systems having only a single CPU with a single core (basically most consumer-grade IBM PC-compatible computers up to the generation of the Pentium® CPUs which had the so-called "hyper-threading" technology), but those systems were routinely running hundreds to thousands of OS threads on a "single core" (the term did not really existed in mainstream then but OK).

Another thing to consider is that your program does not run in isolation: there are other programs running on the same CPU, and the kernel itself has several in-kernel threads as well. You may use a tool like top or htop to assess the number of threads your system is currently scheduling across all your cores.

By this time, you might be wondering why the Go runtime defaults to creating as many threads to power the goroutines as there are physical cores.

Presumably, this comes from a simple fact that in a typical server-side workload, your program will be sort of "the main one". In other words, the contention of its threads with the threads of other processes and the kernel will be reasonably low.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM