drk49438 2016-03-28 11:35
浏览 69
已采纳

需要Goroutine示例说明

I've just started to learn Go and follow a tutorial which contains the following example on goroutines:

package main

import (
    "fmt"
    "runtime"
)

func say(s string) {
    for i := 0; i < 5; i++ {
        runtime.Gosched()
        fmt.Println(s)
    }
}

func main() {
    go say("world") // create a new goroutine
    say("hello")    // current goroutine
}

it states that "runtime.Gosched() means let the CPU execute other goroutines, and come back at some point.". The following output is given below the example:

hello
world
hello
world
hello
world
hello
world
hello

However when I run this example on my machine with go run I get

hello
world
world
world
world
world
hello
hello
hello
hello

my version of Go is go version go1.6 darwin/amd64.

Actually I don't understand either result! Why isn't it just

hello

? As I understand Go programs quit after the last statement of the program is executed, so I'd think that after say() is run as a goroutine and its execution is delayed, the program executes next say() as an ordinary function, print "hello" and then exit.

So which result is the correct one and why?

  • 写回答

1条回答 默认 最新

  • dstk51319 2016-03-28 12:40
    关注

    First output is the one that will be generated by a single core machine. The second could be generated by a multicore one.

    say is a function with a for loop inside, that iterates 5 times. It indeed is an ordinary function but there is a call to Gosched in it. What Gosched does is, it tell the runtime to pause executing the current goroutine and instead start another waiting goroutine. This is called yielding.

    Explaining first output

    This is the output you can expect to get in a single core machine. Going step by step,

    go say("world")

    At this step the runtime starts executing the say("world") call on a seperate goroutine and continue the main goroutine. But the machine only has a single core. So both goroutines cannot run parrallely. The new goroutine (say gr A) has to wait until the running main goroutine(say gr B) finishes or pauses(yeilds). So it waits. Main goroutine starts executing say("hello")

    Now while going through the say function of gr B runtime meets runtime.Gosched()

    The Gosched call is like pausing. It tells the runtime to pause me and shedule an other goroutine that is waiting. So the runtime schedules the gr A. It begins from where it was waiting, which is,

    say("world")

    Now gr A executes until it meets its own runtime.Gosched(). gr A pauses. gr B wakes up and start running from where it left. The statement after runtime.Gosched() is to print "hello". So "hello" is printed. gr B continues and enters the next iteration of its for loop. Meets Gosched. Pauses. gr A restarts. Prints "world". I think you can see how this goes on for 5 times until it prints the given output.

    Explaining the second output

    If your machine has more than one core, the goroutines can run in parallel. Yours is an output you get when it does.

    Now when go say("world") is called gr A does not have to wait until gr B finishes. It can immediately start on an other core. So when Gosched is called there could be no waiting goroutines. If the current one pauses it will immediately start on a different core.

    So in a multicore machine you can't guarantee the order the words are printed. If you run the program many times I think you will see other orders as well.

    You can set GOMAXPROCs to 1 and see how the program would run on a single core machine.

    func main() {
        runtime.GOMAXPROCS(1)
    
        go say("world") // create a new goroutine
        say("hello")    // current goroutine
    }
    

    Then you will see the first output.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?