Consider the following code which launches two goroutines. Each
goroutine uses a for loop to print messages. 

package main

import (
    "fmt"
    "runtime"
    "time"
)

func main() {
  count := runtime.GOMAXPROCS(1)
  fmt.Printf("GOMAXPROCS: %d\n", count)

  // goroutine 1
  go func() {
    for {
      fmt.Printf("1")
    }
  }()

  // goroutine 2
  go func() {
    for {
      fmt.Printf("2")
    }
  }()

  time.Sleep(20 * time.Second)
}

The code uses runtime.GOMAXPROCS(1) to make the Go runtime has only
one processor (P). So there is only one OS thread (M) to run
goroutines.  Without preemption, the M will run goroutine 1 or
goroutine 2 continuously. The messages sent to the terminal should be
all 1s or all 2s. But running the code prints interleaved 1s and 2s to
the terminal, which means that some preemption happens. But
https://www.quora.com/How-does-the-golang-scheduler-work/answer/Ian-Lance-Taylor
says that:

> A G can only be pre-empted at a safe point, which in the current
> implementation can only happen when the code makes a function call. 

Since both goroutines do not make any function calls, there should be
some other way to preempt a G. Can anyone give me an explanation of
such kind of preemption?

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to