duandi5328 2015-07-08 14:43
浏览 42
已采纳

为什么我所谓的并行go程序不并行

package main

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

var wg sync.WaitGroup

func alphabets() {
    for char := 'a'; char < 'a'+26; char++ {
        fmt.Printf("%c ", char)
    }
    wg.Done() //decrement number of goroutines to wait for
}

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

func main() {
    runtime.GOMAXPROCS(2)
    wg.Add(2) //wait for two goroutines

    fmt.Println("Starting Go Routines")
    go alphabets()
    go numbers()

    fmt.Println("
Waiting To Finish")

    wg.Wait() //wait for the two goroutines to finish executing

    fmt.Println("
Terminating Program")
}

I expect the output to be mixed up(for lack of a better word), but instead; a sample output is:

$ go run parallel_prog.go

Starting Go Routines Waiting To Finish 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 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 Terminating Program

What I'm I missing?

Thanks,

  • 写回答

2条回答 默认 最新

  • du4822 2015-07-08 15:41
    关注

    You're missing nothing. It's working. The calls aren't showing up "interlaced" (mixed up) not because they're not being parallelized, but because they're happening really fast.

    You can easily add some calls to time.Sleep to see the parallelization better. By sleeping, we know 100% that printing alphabets and numbers should be interlaced.

    Your program with Sleep calls to "force" interlacing

    package main
    
    import (
        "fmt"
        "sync"
        "time"
    )
    
    var wg sync.WaitGroup
    
    func alphabets() {
        defer wg.Done()
        for char := 'a'; char < 'a'+26; char++ {
            fmt.Printf("%c ", char)
            time.Sleep(time.Second * 2)
        }
    }
    
    func numbers() {
        defer wg.Done()
        for number := 1; number < 27; number++ {
            fmt.Printf("%d ", number)
            time.Sleep(time.Second * 3)
        }    
    }
    
    func main() {
        fmt.Println("Starting Go Routines")
        wg.Add(2)
        go alphabets()
        go numbers()
    
        fmt.Println("
    Waiting To Finish")
        wg.Wait()
        fmt.Println("
    Terminating Program")
    }
    

    Note

    You probably already know this, but setting GOMAXPROCS doesn't have any effect on whether or not this example is executed in parallel, just how many resources it consumes.

    The GOMAXPROCS setting controls how many operating systems threads attempt to execute code simultaneously. For example, if GOMAXPROCS is 4, then the program will only execute code on 4 operating system threads at once, even if there are 1000 goroutines. The limit does not count threads blocked in system calls such as I/O.

    Source: Go 1.5 GOMAXPROCS Default

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

报告相同问题?

悬赏问题

  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符