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 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)