dongpi9494 2017-08-20 16:49
浏览 17
已采纳

Golang平行

I am new to Golang and trying to understand Concurrency and Parallel. I read below mentioned article about Concurrency and Parallel. I have executed same program. But not getting the same ( mixed letter & character ) output. Getting first all letters then characters. it seems like concurrency is working not Parallel is not.

Article says add runtime.GOMAXPROCS(4) to make parallel.

Why I am not getting parallel out ?

I am using 4 core CPU system and GO version 1.8.2

https://www.goinggo.net/2014/01/concurrency-goroutines-and-gomaxprocs.html

I know that if we add Sleep then i can see parallel output, as per Concurrency concept . But Parallelism says if system has more than one CPU, then each thread will run in one CPU and it becomes Parallel process. My question here is Why I am not getting parallel output though my system has 4 core and added runtime.GOMAXPROCS(4), .

Go Program

package main

import (
    "fmt"
    "runtime"
    "sync"
)

func main() {
    runtime.GOMAXPROCS(2)

    var wg sync.WaitGroup
    wg.Add(2)

    fmt.Println("Starting Go Routines")
    go func() {
        defer wg.Done()

        for char := ‘a’; char < ‘a’+26; char++ {
            fmt.Printf("%c ", char)
        }
    }()

    go func() {
        defer wg.Done()

        for number := 1; number < 27; number++ {
            fmt.Printf("%d ", number)
        }
    }()

    fmt.Println("Waiting To Finish")
    wg.Wait()

    fmt.Println("
Terminating Program")
}

My Output

Starting Go Routines
Waiting To Finish
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 a
b c d e f g h i j k l m n o p q r s t u v w x y z
Terminating Program

Expected Output ( not the same , But Parallel output )

Starting Go Routines
Waiting To Finish
a b 1 2 3 4 c d e f 5 g h 6 i 7 j 8 k 9 10 11 12 l m n o p q 13 r s 14
t 15 u v 16 w 17 x y 18 z 19 20 21 22 23 24 25 26
Terminating Program
  • 写回答

3条回答 默认 最新

  • duan6301 2017-08-20 23:55
    关注

    Since for loop has condition 27 in my above example, program execution was fast and not able to see parallelism. I have written different example and increased the condition then I could see the Parallel output.

    package main
    
    import (
        "fmt"
        "runtime"
        "sync"
    )
    
    var wg sync.WaitGroup
    
    func init() {
        fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!", runtime.NumCPU())
    
    }
    func main() {
        fmt.Println(" Welcome to concurrency")
        wg.Add(2)
        go one()
        go two()
        fmt.Println("Waiting To Finish")
        wg.Wait()
        fmt.Println("
    Terminating Program")
    
    }
    
    func one() {
        for i := 0; i < 200; i++ {
            fmt.Println("one", i)
        }
        defer wg.Done()
    }
    
    func two() {
        for i := 0; i < 200; i++ {
            fmt.Println("Two:", i)
        }
        defer wg.Done()
    }
    

    UPDATE 8/28/2015: Go 1.5 is set to make the default value of GOMAXPROCS the same as the number of CPUs on your machine. I have removed the runtime.GOMAXPROCS(4)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了