Goroutine example explaination needed

Issue

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?

Solution

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.

Answered By – Aruna Herath

Answer Checked By – Dawn Plyler (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.