dsxsou8465 2018-03-04 21:09
浏览 125
已采纳

如何在Golang中实现适当的并行性? goroutine是否与Go1.5 +并行?

I'm certainly trying to learn Golang. I know the differences between parallelism and concurrency. I basically look for how to do great parallelism. I expected goroutines to allow me to do that in some way. However, it looks like since 1.5 GOMAXPROCS is set to the number of cores. Does that mean that since 1.5 goroutines are basically parallel!?

Every question I research on sites like StackOverflow are mostly outdated, and don't take into account the 1.5 change. See: Parallel processing in golang

Running the following code doesn't achieve parallelism in Go 1.10: https://play.golang.org/p/24XCgOf0jy5

Setting up GOMAXPROCS to 2 doesn't change the output, purely concurrent instead of parallel.

I'm running this on a FX-6300 8 core system.

Edit: For future reference in case someone encounters this question:

  • I got carried away by this blog: https://www.ardanlabs.com/blog/2014/01/concurrency-goroutines-and-gomaxprocs.html where parallelism is fully done without much trouble in a small for-loop.

  • I should've also pointed out that I'm using the playground to share code, not to actually run it there.

  • The answer by @peterSO is completely valid, for whatever reason, in Go 1.10 the same behaviour as the blog can not be fully replicated.

  • If you want to learn more, check out @kostix comment on the main question, that has provided updated information (as of 03/18) on the topic pointing out that goroutines are fully parallel since the very start of Go, the only difference with 1.5 was the GOMAXPROCS setting.

  • 写回答

1条回答 默认 最新

  • dszm02606009 2018-03-04 21:47
    关注

    The Go Playground is a single-processor virtual machine. You are running a trivial goroutine. Toy programs run on toy machines get toy results.

    Run this on a multiple CPU machine:

    package main
    
    import (
        "fmt"
        "runtime"
        "sync"
    )
    
    var wg sync.WaitGroup
    
    func count() {
        defer wg.Done()
        for i := 0; i < 10; i++ {
            fmt.Println(i)
            i := 0
            for ; i < 1e6; i++ {
            }
            _ = i
        }
    }
    
    func main() {
        fmt.Println("Version", runtime.Version())
        fmt.Println("NumCPU", runtime.NumCPU())
        fmt.Println("GOMAXPROCS", runtime.GOMAXPROCS(0))
        wg.Add(2)
        go count()
        go count()
        wg.Wait()
    }
    

    Output:

    Version go1.10
    NumCPU 8
    GOMAXPROCS 8
    0
    0
    1
    1
    2
    2
    3
    3
    4
    4
    5
    5
    6
    6
    7
    7
    8
    8
    9
    9
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同