Skip to content

runtime: Different behavior of go routines in go1.5beta2 on MacOS and Linux #11848

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
matyunin opened this issue Jul 23, 2015 · 2 comments
Closed

Comments

@matyunin
Copy link

The example is taken from the "A Tour of Go": https://tour.golang.org/concurrency/1

Obviously, program output should have 10 rows: 5 for "hello" and 5 for "world".

But we have on:

  • Linux - 9 rows
  • MacOS - 10 rows

Linux output (9 rows):

$ go run 1.go 
hello
world
hello
world
hello
world
world
hello
hello

MacOS X output (10 rows):

$ go run 1.go 
hello
world
world
hello
hello
world
hello
world
hello
world

Can anyone explain - why?

Linux uname -a:

Linux desktop 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt11-1 (2015-05-24) x86_64 GNU/Linux

MacOS X uname -a:

Darwin 14.5.0 Darwin Kernel Version 14.5.0: Thu Jul  9 22:56:16 PDT 2015; root:xnu-2782.40.6~1/RELEASE_X86_64 x86_64

Source code from tour:

package main

import (
    "fmt"
    "time"
)

func say(s string) {
    for i := 0; i < 5; i++ {
        time.Sleep(1000 * time.Millisecond)
        fmt.Println(s)
    }
}

func main() {
    go say("world")
    say("hello")
}
@crawshaw
Copy link
Member

This program does not necessarily output ten rows. What happens is: it starts say("world") in a background goroutine, executes say("hello"), and then exits. In practice, because the sleep is so long, the two likely scenarios are:

  • say("world") completing before say("hello"), in which case you get 10 lines, or
  • say("hello") completing before the final loop of say("world"), in which case you get 9 lines.

(On a machine starved for CPU you could see fewer "world" lines, but you would have to construct such a case.)

@matyunin
Copy link
Author

@crawshaw thanks :) 👍

@mikioh mikioh changed the title Different behavior in go1.5beta2 on MacOS and Linux runtime: Different behavior of go routines in go1.5beta2 on MacOS and Linux Jul 24, 2015
@golang golang locked and limited conversation to collaborators Aug 5, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants