Junior — Middle
Millised tagajärjed on programmi tööle, kui GOMAXPROCS on seatud väärtusele 1 ja käivitatakse lõpmatu goroutine?
sobes.tech AI
Vastus AI-lt
Setting GOMAXPROCS to 1 limits the number of operating system threads that the Go scheduler can use to one thread. In this scenario:
- All computations will be executed sequentially on a single thread.
- If the infinite goroutine does not contain points where a context switch occurs (e.g., blocking calls or
runtime.Gosched()), other goroutines may not get execution time, leading to the program "hanging". - As a result, the program may become unresponsive or run very slowly, since the scheduler cannot efficiently switch between goroutines.
Example:
package main
import (
"runtime"
"time"
"fmt"
)
func main() {
runtime.GOMAXPROCS(1)
go func() {
for {
// infinite loop without calling runtime.Gosched()
}
}()
time.Sleep(time.Second)
fmt.Println("Program finished")
}
In this example, the main goroutine may not get execution time if the infinite goroutine does not yield control, which will cause the program to "hang".