On Wed, 8 Mar 2017 06:20:25 -0800 (PST)
asci...@gmail.com wrote:

> I've made this test program
> 
> 
> package main
> 
> import "fmt"
> 
> func func1() {
>     fmt.Println("running func1")
>     for {
> 
>     }
[...]
> func main() {
> 
>     go func1()
>     fmt.Println("run func1")
>     go func2()
>     fmt.Println("run func2")
>     go func3()
>     fmt.Println("run func3")
>     for {
> 
>     }
> 
> }
[...]

Each for { } produces a "busy loop" which burns a single CPU
core/socket and effectively binds a goroutine it executes in to its
underlying OS thread.

This means that if your Linux VM has different idea of CPU
cores/sockets available than your Windows host (supposedly, fewer), you
will get different printouts because certain goroutines simply won't
have a chance to run.

Please read up any Go book / HOWTO on goroutine synchronization
techniques and use any of them to properly synchronize your main() with
your goroutines (sync.WaitGroup might be the simplest one for this
case).  Don't ever use busy loops.

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