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

如何在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.

图片转代码服务由CSDN问答提供 功能建议

我当然是在学习Golang。 我知道并行和并发之间的区别。 我基本上是在寻找如何实现出色的并行性。 我希望goroutines可以让我以某种方式做到这一点。 但是,由于将1.5 GOMAXPROCS设置为内核数,因此看起来。

我在StackOverflow之类的网站上研究的每个问题大部分都已经过时,并且没有考虑1.5的变化,这是否意味着既然goroutine基本上是并行的!!

请参阅:使用golang进行并行处理

运行以下命令 代码无法在Go 1.10中实现并行性: https://play.golang.org/p/24XCgOf0jy5 < / a>

将GOMAXPROCS设置为2不会更改输出,纯并发而不是并行。

我在 FX-6300 8核心系统。

编辑:如果有人遇到此问题,以供将来参考:

  • 我被这个博客所吸引: https://www.ardanlabs.com/blog/2014/01/concurrency-goroutines-and-gomaxprocs.html ,在一个小的for循环中完全完成了并行操作而没有很多麻烦。

  • 我还应该指出,我正在使用游乐场共享代码,而不是

  • 由于任何原因,@ peterSO的回答是完全有效的,无论出于何种原因,在Go 1.10中,都无法完全复制与博客相同的行为。 / p>

  • 如果您想了解更多信息,请查看有关主要问题的@kostix评论,该评论提供了有关该主题的最新信息(截至03/18)。 自Go以来,goroutines就完全并行了,与1.5的唯一区别是GOMAXPROCS设置。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题