What exactly does runtime.Gosched do?

In a version prior to the release of go 1.5 of the Tour of Go website, there’s a piece of code that looks like this.

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")
    say("hello")
}

The output looks like this:

hello
world
hello
world
hello
world
hello
world
hello

What is bothering me is that when runtime.Gosched() is removed, the program no longer prints “world”.

hello
hello
hello
hello
hello

Why is that so? How does runtime.Gosched() affect the execution?

1 Like

When you run Go program without specifying GOMAXPROCS environment variable, Go goroutines are scheduled for execution in single OS thread. However, to make program appear to be multithreaded (that’s what goroutines are for, aren’t they?), the Go scheduler must sometimes switch the execution context, so each goroutine could do its piece of work.

As I said, when GOMAXPROCS variable is not specified, Go runtime is only allowed to use one thread, so it is impossible to switch execution contexts while goroutine is performing some conventional work, like computations or even IO (which is mapped to plain C functions). The context can be switched only when Go concurrency primitives are used, e.g. when you switch on several chans, or (this is your case) when you explicitly tell the scheduler to switch the contexts - this is what runtime.Gosched is for.

So, in short, when execution context in one goroutine reaches Gosched call, the scheduler is instructed to switch the execution to another goroutine. In your case there are two goroutines, main (which represents ‘main’ thread of the program) and additional, the one you have created with go say . If you remove Gosched call, the execution context will never be transferred from the first goroutine to the second, hence no ‘world’ for you. When Gosched is present, the scheduler transfers the execution on each loop iteration from first goroutine to the second and vice versa, so you have ‘hello’ and ‘world’ interleaved.

FYI, this is called ‘cooperative multitasking’: goroutines must explicitly yield the control to other goroutines. The approach used in most contemporary OSes is called ‘preemptive multitasking’: execution threads are not concerned with control transferring; the scheduler switches execution contexts transparently to them instead. Cooperative approach is frequently used to implement ‘green threads’, that is, logical concurrent coroutines which do not map 1:1 to OS threads - this is how Go runtime and its goroutines are implemented.

Update

I’ve mentioned GOMAXPROCS environment variable but didn’t explain what is it. It’s time to fix this.

When this variable is set to a positive number N , Go runtime will be able to create up to N native threads, on which all green threads will be scheduled. Native thread a kind of thread which is created by the operating system (Windows threads, pthreads etc). This means that if N is greater than 1, it is possible that goroutines will be scheduled to execute in different native threads and, consequently, run in parallel (at least, up to your computer capabilities: if your system is based on multicore processor, it is likely that these threads will be truly parallel; if your processor has single core, then preemptive multitasking implemented in OS threads will create a visibility of parallel execution).

It is possible to set GOMAXPROCS variable using runtime.GOMAXPROCS() function instead of pre-setting the environment variable. Use something like this in your program instead of the current main :

func main() {
    runtime.GOMAXPROCS(2)
    go say("world")
    say("hello")
}

In this case you can observe interesting results. It is possible that you will get ‘hello’ and ‘world’ lines printed interleaved unevenly, e.g.

hello
hello
world
hello
world
world
...

This can happen if goroutines are scheduled to separate OS threads. This is in fact how preemptive multitasking works (or parallel processing in case of multicore systems): threads are parallel, and their combined output is indeterministic. BTW, you can leave or remove Gosched call, it seems to have no effect when GOMAXPROCS is bigger than 1.

The following is what I got on several runs of the program with runtime.GOMAXPROCS call.

hyperplex /tmp % go run test.go
hello
hello
hello
world
hello
world
hello
world
hyperplex /tmp % go run test.go
hello
world
hello
world
hello
world
hello
world
hello
world
hyperplex /tmp % go run test.go
hello
hello
hello
hello
hello
hyperplex /tmp % go run test.go
hello
world
hello
world
hello
world
hello
world
hello
world

See, sometimes output is pretty, sometimes not. Indeterminism in action :slight_smile:

Another update

Looks like that in newer versions of Go compiler Go runtime forces goroutines to yield not only on concurrency primitives usage, but on OS system calls too. This means that execution context can be switched between goroutines also on IO functions calls. Consequently, in recent Go compilers it is possible to observe indeterministic behavior even when GOMAXPROCS is unset or set to 1

Refer: stackoverflow.com