This is the 19th day of my participation in the Genwen Challenge
How to control the number of cpus?
Multiple cpus can execute multiple coroutines at the same time. This is controlled by changing the GOMAXPROCS parameter setting, which defaults to the number of cores on the current CPU. If parallel execution is possible, the program should be implemented on a multi-CPU machine. To change the number of parallel cpus used, set the environment variable or configure it using a similar naming feature of the Runtime package to support a different number of threads. Setting it to 1 disables parallelism and forces Goroutines to take turns (serialized).
The runtime can allocate more threads than the value of GomaxProcs to serve multiple outstanding I/O requests. GomaxProcs only affects the number of Goroutines executed at a time. Arbitrary parameter Settings can cause system calls to block.
Go’s Goroutine scheduling isn’t perfect yet, although it’s improving all the time. In the future, it will better optimize the OS thread usage aspect. For now, if you have a performance problem, it may be helpful to set GomaxProcs on the basis of your application.
Why is there no Goroutine ID?
Goroutines don’t have names. They’re anonymous. It has no unique identifiers, names, or data structures. Some were surprised by this and expected the Go declaration to return some items that could be used to access and control Goroutine.
The basic reason is that anonymous Goroutines can use the fully functional Go language when writing concurrent code, which, in turn, can limit the libraries used to write code. (PS: Why?)
I don’t think the original is very well written. It’s really not good. Baidu other blogger wrote the reason, feel more valuable, we can refer to see.
According to official sources, Go deliberately does not provide goid in order to avoid abuse. This is because most users will unconsciously write codes that rely heavily on GOID after they get it easily. A strong reliance on goIDS makes this code not portable and complicates the concurrency model. At the same time, a large number of Goroutines can exist in Go at the same time, but it is not easy to monitor when each Goroutine is destroyed in real time, which makes it difficult for goID-dependent resources to be recycled automatically (manually).
Anonymous illustrationNCopy the code