overexchange
overexchange

Reputation: 1

Does Go runtime create OS threads(M)?

numLogicalProcessors on Intel core i7 is 8(2 X 4 physical cores). Linux OS. So, eight OS threads(M) can work in parallel. Go runtime can assign eight contexts(P1, P2....P8 - runtime.GOMAXPROCS(numLogicalProcessors)) in my Go program.

enter image description here

Go follows M:N threading model, where N are OS threads and M are go routines of a Go program.

OS scheduler schedules OS threads. Thread states are WAITING, RUNNABLE & EXECUTING.

Go scheduler schedules Go routines. Go-routine states are WAITING, RUNNABLE & EXECUTING. Goroutine is a user level thread.


  1. Does the runtime of a Go program explicitly create those eight OS threads(M)? before assigning each context(P) to each OS thread(M)?

  2. If OS thread(M1) is pre-empted(due to time-slice) by OS scheduler, How does goroutine scheduler(P1) manage the state of goroutine G1 using LRQ? Does P1 get the notification from OS that M1 state has changed?

Upvotes: 2

Views: 1608

Answers (1)

Grzegorz Żur
Grzegorz Żur

Reputation: 49241

  1. Yes, Go scheduler start execution threads. The number of them can be examined or changed with runtime.GOMAXPROCS.
  2. No, operating system preemption is transparent to the running process. Go runtime since version 1.14 can preempt Go routines but this is to avoid locking threads by tight loops. It is not related to operating system preemption.

Upvotes: 3

Related Questions