详解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的数目

阅读剩余部分

相关阅读 >>

聊聊dubbo-go-proxy的consulregistryload

go2设计草案介绍

我的个人能力发展报告(2015-2019)

golang可以调用lua吗?

手撸golang 结构型设计模式 享元模式

go 中基于 ip 地址的 http 请求限流

golang io读取文件与判断文件是否存在的方法

ketos 笔记 -- 记 go hackathon 2017

使用gin封装一个web脚手架(八):限流器

手撸golang go与微服务 聚合模式之1

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




打赏

取消

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

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

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

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

评论

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