Middle+
What type of multitasking is used in Go and what type was used before Go 1.15?
sobes.tech AI
Answer from AI
Golang uses cooperative multitasking with preemption.
Before version Go 1.15, only cooperative multitasking without preemption was used.
Differences:
| Type of multitasking | Description |
|---|---|
| Cooperative without preemption | A goroutine voluntarily yields control, calling blocking operations or runtime.Gosched(). If the goroutine does not yield, other goroutines may be blocked. |
| Cooperative with preemption | The scheduler can forcibly interrupt the execution of a goroutine after some time, even if it is not performing a blocking operation. This is achieved by inserting preemption points into cyclic constructs. |
Example of a preemption point in a loop (the Go compiler inserts them automatically in suitable places):
func heavyComputation() {
for i := 0; i < 1000000000; i++ {
// At this point, the compiler may add a check
// for the need to switch context by the scheduler.
_ = i * i
}
}