This is the 14th day of my participation in the August More Text Challenge. For details, see: August More Text Challenge
In my column:Let’s Golang
How does “Golang together” allocate coroutine resources and set the number of available CPU cores
Now that you’ve seen the coroutine basics, seen the CPS concurrency model, and seen Golang’s million-level concurrency, let’s implement the coroutine resource allocation and set the number of CPU cores available.
Transfer coroutine resources
Let’s look at the results:
Subcoroutine 0 0 subcoroutine 0 1 subcoroutine 0 2 subcoroutine 0 3 subcoroutine 0 4 subcoroutine 0 5 subcoroutine 2 1 subcoroutine 2 2 subcoroutine 2 3 subcoroutine 2 4 subcoroutine 2 5 subcoroutine 2 6 subcoroutine 2 7 subcoroutine 1 0 subcoroutine 0 6 subcoroutine 0 7 Subcoroutine 1 1 subcoroutine 1 2 subcoroutine 1 3 subcoroutine 1 4 subcoroutine 1 5 subcoroutine 1 6 subcoroutine 1 7Copy the code
We can see that subcoroutine 0 prints, then word coroutine 2 prints, then subcoroutine 1 prints, then all of a sudden subcoroutine 0 prints, then subcoroutine 1 prints.
Why is that? Take a look at the code and see why!
func main() { for i:=0; i<3; I ++{go func(index int) {task(" subcoroutine "+ strconv.itoa (index))}(I)} time.sleep (5 * time.second)} func task(name string) { for i:=0; i<8; ++{if name == "subcoroutine 1"{// yields the processor, allowing other goroutines to run. It does not suspend the current goroutine, so execution resumes automatically. runtime.Gosched() } fmt.Println(name,i) } }Copy the code
This code activates three subcoroutines, subcoroutine 0, subcoroutine 1, and subcoroutine 2. Then each coroutine executes the task function, but the runtime.gosched () operation is performed on subcoroutine 1.
Let’s introduce Runtime.gosched ().Gosched generates a processor that allows other Goroutines to run first. It does not abort the current Goroutine, so the current goroutine automatically resumes running. What it does is it lowers the priority of the current coroutine.
We know that the three coroutines are concurrent. But here we implement Runtime.gosched () on coroutine 1, which causes subcoroutine 1 to give away coroutine resources. So the final printout must be subcoroutine 1. However, not all subcoroutines 1 are the last to print. There’s a subcoroutine 1 that’s printed out before a subcoroutine 0. Let’s increase the number of coroutines. Change the range of the for loop in the main function from 0 3 to 0 108, and you will find that the output is still subcoroutine 1. (Because of the excessive output, I will not show the output here.) This is because runtime.gosched () reduces the priority of the coroutine and gives away resources, but it does not necessarily make the coroutine absolutely complete.
Set the number of available CPU cores
We open the Task manager, enter the “Performance” column, right-click on the CPU and choose “Change graphics to”, change “overall utilization” to “logical processor”.
Here you can see that the number of CPU cores in the image is eight.
Then we can use the GO language to check the number of computer CPU cores.
Package main import (" FMT "" Runtime") func main() {fmt.println (" number of available CPU cores ", runtime.numCPU ())) // GOMAXPROCS sets the maximum number of CPUs that can be executing // simultaneously and returns the previous setting. If n < 1, it does not // change the current setting. // The number of logical CPUs on the local machine can be queried with NumCPU. // This call will go away when the scheduler improves.fmt. Println(" Set the number of available CPU cores to 1, Previously set to ", runtime.gomaxProcs (1))}Copy the code
Print using runtime.numCPU ().
Runtime.gomaxprocs (n) allows you to set the maximum number of cores to run. Set the maximum number of cores available to the CPU to 1. This function will return the previous maximum number of cores available. If n < 1, the current setting is not changed.