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

如何在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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 R语言读取nc按月均值转为tif
  • ¥15 用这个函数清除字符串空格为什么会出现问题?
  • ¥30 智能车串级pid调参
  • ¥15 visual studio code翻译老是错误
  • ¥20 卫星测高数据的高程转换
  • ¥20 PHP+javascript文本框之间字符串逻辑判断
  • ¥15 爬取招聘网站数据信息
  • ¥15 安装完tensorflow,import tensorflow as tf后报错,如何解决?
  • ¥15 ultralytics库导出onnx模型,模型失去预测能力
  • ¥15 linux下点对点协议连接2个USB串口的硬件流量控制问题