Control flow while using "go" keyword and "runtime.Gosched()" in thr

package main

import (

"fmt"

"runtime"

"sync"

"sync/atomic"

)

func main() {

fmt.Println("NumCPU:", runtime.NumCPU())

fmt.Println("NumGoroutine: First", runtime.NumGoroutine())

var counter int64

const gs = 100

var wg sync.WaitGroup

wg.Add(gs)

for i := 0; i < gs; i++ {

    go func() {

        atomic.AddInt64(&counter, 1)

        runtime.Gosched()

        fmt.Println("Counter:\t", atomic.LoadInt64(&counter))

        wg.Done()

    }()

    fmt.Println("NumGoroutine:", runtime.NumGoroutine())

}

wg.Wait()

fmt.Println("NumGoroutine: Last", runtime.NumGoroutine())

fmt.Println("count:", counter)

}

Hello GO Community, I am new to go since i was learning concurrency in go i came across this piece of code where i cannot get the control flow of the code. I know the output is never the same as everything runs in parallel for this code.

Below is the output i got on my terminal/cmd.

NumCPU: 4
NumGoroutine: First 1
NumGoroutine: 2
Counter: 1
NumGoroutine: 2
Counter: 2
NumGoroutine: 2
Counter: 3
NumGoroutine: 2
Counter: 4
NumGoroutine: 3
Counter: 5
NumGoroutine: 2
Counter: 6
NumGoroutine: 2
Counter: 7
NumGoroutine: 3
Counter: 8
NumGoroutine: 3
Counter: 9
NumGoroutine: 3
Counter: 10
NumGoroutine: 3
Counter: 11
NumGoroutine: 3
Counter: 12
NumGoroutine: 3
Counter: 13
NumGoroutine: 3
Counter: 14
NumGoroutine: 3
Counter: 15
NumGoroutine: 3
Counter: 16
NumGoroutine: 3
Counter: 17
NumGoroutine: 3
Counter: 18
NumGoroutine: 3
Counter: 19
NumGoroutine: 3
Counter: 20
NumGoroutine: 3
Counter: 21
NumGoroutine: 3
Counter: 22
NumGoroutine: 3
Counter: 23
NumGoroutine: 3
Counter: 24
NumGoroutine: 3
Counter: 25
NumGoroutine: 3
Counter: 26
NumGoroutine: 3
Counter: 27
NumGoroutine: 3
Counter: 28
NumGoroutine: 3
Counter: 29
NumGoroutine: 3
Counter: 30
NumGoroutine: 3
Counter: 31
NumGoroutine: 3
Counter: 32
NumGoroutine: 3
Counter: 33
NumGoroutine: 3
Counter: 34
NumGoroutine: 3
Counter: 35
NumGoroutine: 3
Counter: 36
NumGoroutine: 3
Counter: 37
NumGoroutine: 3
Counter: 38
NumGoroutine: 3
NumGoroutine: 3
Counter: 39
Counter: 40
NumGoroutine: 4
Counter: 41
NumGoroutine: 3
Counter: 42
NumGoroutine: 3
Counter: 43
NumGoroutine: 3
Counter: 44
NumGoroutine: 3
Counter: 45
NumGoroutine: 3
Counter: 46
NumGoroutine: 3
Counter: 47
NumGoroutine: 3
NumGoroutine: 3
Counter: 48
Counter: 49
NumGoroutine: 4
Counter: 50
NumGoroutine: 3
Counter: 51
NumGoroutine: 3
Counter: 52
NumGoroutine: 3
Counter: 53
NumGoroutine: 3
Counter: 54
NumGoroutine: 3
NumGoroutine: 3
Counter: 55
Counter: 56
NumGoroutine: 4
Counter: 57
NumGoroutine: 3
Counter: 58
NumGoroutine: 3
Counter: 59
NumGoroutine: 3
Counter: 60
NumGoroutine: 3
NumGoroutine: 3
Counter: 61
Counter: 62
NumGoroutine: 4
Counter: 63
NumGoroutine: 3
NumGoroutine: 3
Counter: 64
Counter: 65
NumGoroutine: 4
Counter: 66
NumGoroutine: 3
Counter: 67
NumGoroutine: 3
Counter: 68
NumGoroutine: 3
Counter: 69
NumGoroutine: 3
Counter: 70
NumGoroutine: 3
Counter: 71
NumGoroutine: 3
Counter: 72
NumGoroutine: 3
Counter: 73
NumGoroutine: 3
Counter: 74
NumGoroutine: 3
Counter: 75
NumGoroutine: 3
Counter: 76
NumGoroutine: 3
Counter: 77
NumGoroutine: 3
Counter: 78
NumGoroutine: 3
Counter: 79
NumGoroutine: 3
Counter: 80
NumGoroutine: 3
Counter: 81
NumGoroutine: 3
Counter: 82
NumGoroutine: 3
Counter: 83
NumGoroutine: 3
Counter: 84
NumGoroutine: 3
Counter: 85
NumGoroutine: 3
Counter: 86
NumGoroutine: 3
Counter: 87
NumGoroutine: 3
Counter: 88
NumGoroutine: 3
Counter: 89
NumGoroutine: 3
Counter: 90
NumGoroutine: 3
Counter: 91
NumGoroutine: 3
Counter: 92
NumGoroutine: 3
Counter: 93
NumGoroutine: 3
Counter: 94
NumGoroutine: 3
Counter: 95
NumGoroutine: 3
Counter: 96
NumGoroutine: 3
Counter: 97
NumGoroutine: 3
Counter: 98
NumGoroutine: 3
Counter: 99
NumGoroutine: 3
Counter: 100
NumGoroutine: Last 1
count: 100

What happens when the program hit the line runtime.Gosched() for the first time where does the control flow go towards.

why do in the 3rd line of output i got NumGoroutine printed prior to count.

Because the goroutine in the loop hasn’t started executing before the line that prints “NumGoroutine” is executed. The goroutine runs asynchronously. You can’t control when it runs compared to other goroutines without explicit synchronization.

1 Like

Thankyou for your time and effort Jeff Emanuel. :smiley:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.