On Fri, Feb 22, 2019 at 9:17 AM Jingguo Yao <yaojing...@gmail.com> wrote:

> Without preemption, the M will run goroutine 1 or goroutine 2
continuously.

Not correct, there's also cooperative scheduling/yielding.

> The messages sent to the terminal should be all 1s or all 2s.

Nothing in the language specs mandates such undesirable behavior.

> Since both goroutines do not make any function calls, there should be
some other way to preempt a G.

Both of the goroutines _do_ call fmt.Printf.

> Can anyone give me an explanation of such kind of preemption?

Cooperative scheduling happens on function entry*. At least that was the
strategy last time I checked. Since that time I think I've heard about
plans for doing it preemptively, but I don't know what's the current status.

*: Actually, the compiler is free to insert such cooperative check at any
point. Another place where it can be inserted is a loop. But again, I don't
know if it is happening in this release, if it's still a only plan or an
already abandoned plan.

-- 

-j

-- 
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