详解Golang cpu的使用设置


本文摘自php中文网,作者藏色散人,侵删。

下面由golang教程栏目给大家详解Golang cpu的使用设置,希望对需要的朋友有所帮助!

以下测试,使用的Go版本是1.8.3

不设置

如果没有调用runtime.GOMAXPROCS 去设置CPU,Golang默认使用所有的cpu核。

测试机器CPU有4个核,测试代码开启4个goroutine,从测试结果看4个核心全部跑满。

测试代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

package main

 

func main()  {

    go task()

    go task()

    go task()

    go task()

 

    select{}

 

}

 

 

func task(){

 

    for {  

     

    }

 

}

设置CPU使用

1

func GOMAXPROCS(n int) int

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.

设置并发执行时使用的CPU的数目

阅读剩余部分

相关阅读 >>

go - 实现项目内链路追踪(二)

难以遏制的人因差错 -go 的日志工具之痛

分布式定时任务执行器

golang怎么判断数组是否为空

golang base64 斜杠 等号 问题

dbatool-dodba

go开源说第四期:go-zero解读与最佳实践(上)

go log

golang有类(class)吗?

golang—literal copies lock value from gzpool: sync.pool contains sync.nocopy

更多相关阅读请进入《golang》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...

    暂无评论...