Sobes.tech
Senior

Is a new thread created for each goroutine in the operating system?

sobes.tech AI

Answer from AI

No, a separate operating system thread is not created for each goroutine in Go. A goroutine can be considered as a lightweight thread, managed by the Go runtime scheduler.

The Go runtime uses an M:N model, where M is the number of OS threads and N is the number of goroutines. The scheduler distributes many goroutines across fewer OS threads, efficiently utilizing system resources.

This allows creating thousands or even millions of goroutines with minimal overhead, unlike system threads, which are more heavyweight.

Thus, goroutines are cooperatively scheduled tasks within one or more OS threads, not a 1:1 correspondence.