Skip to content

Instantly share code, notes, and snippets.

@d-led
Last active July 8, 2020 12:44
Show Gist options
  • Save d-led/066b7a9b8ce52e345e62212716daa02a to your computer and use it in GitHub Desktop.
Save d-led/066b7a9b8ce52e345e62212716daa02a to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"runtime"
"time"
)
func main() {
go tick()
count := 1
for {
go spin()
fmt.Printf("Started task %v\n", count)
time.Sleep(2 * time.Second)
count++
}
runtime.Goexit()
}
func tick() {
i := 0
for {
fmt.Printf("Tick %v\n", i)
time.Sleep(1 * time.Second)
i++
}
}
func spin() {
i := 0
for {
i++
// runtime.Gosched() // yield...
}
}
@d-led
Copy link
Author

d-led commented Dec 21, 2017

go

see how the scheduler threads are exhausted

@d-led
Copy link
Author

d-led commented Dec 21, 2017

to give up control gracefully

		if i%1000000000 == 0 {
			fmt.Printf("Task: %v\n", id)
		}

@d-led
Copy link
Author

d-led commented Jul 8, 2020

seems not to be the case anymore: go1.14.3

...
Started task 50
Tick 97
Tick 98
Started task 51
Tick 99
Tick 100
Started task 52
Tick 101
Tick 102
Started task 53
Tick 103
Tick 104
Started task 54
Tick 105
Tick 106
Started task 55
Tick 107
Tick 108
Started task 56
Tick 109

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment