golang
just has a method runtime.GOMAXPROCS(1)
to set the application run one goroutine
at the same time,But i want to let the application just run on the specificone cpu?
To launch you Go binary, say example.exe
so that it runs only on CPU 0 on Windows, you can use start
command with "affinity" parameter:
start /affinity 1 example.exe
I don't know what exactly are you trying to achieve but refer to this document describing GOMAXPROCS - it might be the case that Go runtime will do the same thing more efficiently.
Under the gc compilers (6g or 8g) you must set GOMAXPROCS to more than the default value 1 to allow the run-time support to utilize more than one OS thread, that is all goroutines share the same thread unless GOMAXPROCS is set to a value greater than 1.
When GOMAXPROCS is greater than 1, they run on a thread pool with that many threads. With the gccgo compiler GOMAXPROCS is effectively equal to the number of running goroutines. Suppose n
is the number of processors or cores on the machine. If you set the environment variable GOMAXPROCS >= n
, or call runtime.GOMAXPROCS(n)
, then the goroutines are divided (distributed) among the n processors.
More processors however don’t mean necessarily a linear improvement in performance, mainly because more communication is needed: the message-passing overhead increases. An experiential rule of thumb seems to be that for n cores setting GOMAXPROCS to n-1 yields the best performance, and the following should also be followed:
number of goroutines > 1 + GOMAXPROCS > 1
So if there is only one goroutine executing at a certain point in time, don’t set GOMAXPROCS!
If you wish to lock a goroutine to a particular OS thread you can do this with runtime.LockOSThread.
LockOSThread wires the calling goroutine to its current operating system thread. Until the calling goroutine exits or calls UnlockOSThread, it will always execute in that thread, and no other goroutine can.
More about on https://golang.org/pkg/runtime/.