FAQ
Hi all,

Today I read about the go memory model. When I am reading the "Channel
communication" chapter, I come up with a question:

Think of below two code:

code 1:

var c = make(chan int)

func f() {
c <- 0
print("hello, world")
}

func main() {
go f()
<-c
}

code 2:

var c = make(chan int)

func f() {
<-c
print("hello, world")
}

func main() {
go f()
c <- 0
}

In these two cases, will the "hello world" be guaranteed to pe print?
According to the MM doc, it only guarantees the <-c HB c <-0 vice versa.

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Kevin Gillette at Mar 7, 2013 at 9:13 am
    The phrase from the memory model spec it seems you're looking for is "A
    send on a channel happens before the corresponding receive from that
    channel completes." However, this has no bearing on your two sample
    programs. The send in f() of program 1 is guaranteed to complete before the
    program exits, but the receive in f() of program 2 is not guaranteed to
    complete before the program exits. The actual print calls both occur after
    the channel communications occur, but there no guarantee that the program
    will not exit before those occur, especially if GOMAXPROCS == 1. An
    implementation is under no requirement to schedule f's goroutine in program
    2 as soon as the communication occurs, or for program 1, the scheduler
    could mux the main goroutine immediately during communication; it seems
    with any deterministic scheduler, one of those programs will have almost no
    chance of output with GOMAXPROCS of 1, and in any case with a higher
    GOMAXPROCS it's a race condition.
    On Thursday, March 7, 2013 12:36:16 AM UTC-7, Totoro Ghibli wrote:

    Hi all,

    Today I read about the go memory model. When I am reading the "Channel
    communication" chapter, I come up with a question:

    Think of below two code:

    code 1:

    var c = make(chan int)

    func f() {
    c <- 0
    print("hello, world")
    }

    func main() {
    go f()
    <-c
    }

    code 2:

    var c = make(chan int)

    func f() {
    <-c
    print("hello, world")
    }

    func main() {
    go f()
    c <- 0
    }

    In these two cases, will the "hello world" be guaranteed to pe print?
    According to the MM doc, it only guarantees the <-c HB c <-0 vice versa.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Tw at Mar 7, 2013 at 9:35 am

    On 03/07/2013 05:13 PM, Kevin Gillette wrote:
    The phrase from the memory model spec it seems you're looking for
    is "A send on a channel happens before the corresponding receive from
    that channel completes." However, this has no bearing on your two
    sample programs. The send in f() of program 1 is guaranteed to
    complete before the program exits, but the receive in f() of program 2
    is not guaranteed to complete before the program exits. The actual
    print calls both occur after the channel communications occur, but
    there no guarantee that the program will not exit before those occur,
    especially if GOMAXPROCS == 1. An implementation is under no
    requirement to schedule f's goroutine in program 2 as soon as the
    communication occurs, or for program 1, the scheduler could mux the
    main goroutine immediately during communication; it seems with any
    deterministic scheduler, one of those programs will have almost no
    chance of output with GOMAXPROCS of 1, and in any case with a higher
    GOMAXPROCS it's a race condition.

    On Thursday, March 7, 2013 12:36:16 AM UTC-7, Totoro Ghibli wrote:

    Hi all,

    Today I read about the go memory model. When I am reading the
    "Channel communication" chapter, I come up with a question:

    Think of below two code:

    code 1:

    var c = make(chan int)

    func f() {
    c <- 0
    print("hello, world")
    }

    func main() {
    go f()
    <-c
    }

    code 2:

    var c = make(chan int)

    func f() {
    <-c
    print("hello, world")
    }

    func main() {
    go f()
    c <- 0
    }

    In these two cases, will the "hello world" be guaranteed to pe
    print? According to the MM doc, it only guarantees the <-c HB c
    <-0 vice versa.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    Sorry for previous mail.

    Thanks to reply. But I am not sure with this statement:

    /", but the receive in f() of program 2 is not guaranteed to
    complete before the program exits./"

    The MM doc says :
    A receive from an unbuffered channel happens before the send on that
    channel completes."
    I think these two statement are inconsistent.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Maxim Khitrov at Mar 7, 2013 at 11:33 am

    On Thu, Mar 7, 2013 at 4:35 AM, Tw wrote:
    On 03/07/2013 05:13 PM, Kevin Gillette wrote:

    The phrase from the memory model spec it seems you're looking for is "A send
    on a channel happens before the corresponding receive from that channel
    completes." However, this has no bearing on your two sample programs. The
    send in f() of program 1 is guaranteed to complete before the program exits,
    but the receive in f() of program 2 is not guaranteed to complete before the
    program exits. The actual print calls both occur after the channel
    communications occur, but there no guarantee that the program will not exit
    before those occur, especially if GOMAXPROCS == 1. An implementation is
    under no requirement to schedule f's goroutine in program 2 as soon as the
    communication occurs, or for program 1, the scheduler could mux the main
    goroutine immediately during communication; it seems with any deterministic
    scheduler, one of those programs will have almost no chance of output with
    GOMAXPROCS of 1, and in any case with a higher GOMAXPROCS it's a race
    condition.
    On Thursday, March 7, 2013 12:36:16 AM UTC-7, Totoro Ghibli wrote:

    Hi all,

    Today I read about the go memory model. When I am reading the "Channel
    communication" chapter, I come up with a question:

    Think of below two code:

    code 1:

    var c = make(chan int)

    func f() {
    c <- 0
    print("hello, world")
    }

    func main() {
    go f()
    <-c
    }

    code 2:

    var c = make(chan int)

    func f() {
    <-c
    print("hello, world")
    }

    func main() {
    go f()
    c <- 0
    }

    In these two cases, will the "hello world" be guaranteed to pe print?
    According to the MM doc, it only guarantees the <-c HB c <-0 vice versa.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.



    Sorry for previous mail.

    Thanks to reply. But I am not sure with this statement:

    ", but the receive in f() of program 2 is not guaranteed to complete before
    the program exits."

    The MM doc says :
    A receive from an unbuffered channel happens before the send on that channel
    completes."
    I think these two statement are inconsistent.
    The doc is correct. The program is not guaranteed to print "hello,
    world", but the receive in f() is guaranteed to happen before main
    exits (as long as c is an unbuffered channel). Main can't proceed
    beyond c <- 0 until the value is received in f(). If c was buffered,
    then f() may never get a chance to run.

    - Max

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Kevin Gillette at Mar 7, 2013 at 4:58 pm
    True, though print still always happens after the communication, as does
    the end of the main function. The MM says nothing about the relative
    ordering of those two situations since they are not related (and thus not
    relative to each other). At best, it's a race condition, and you cannot
    count on anything being output.
    On Thursday, March 7, 2013 2:35:27 AM UTC-7, Totoro Ghibli wrote:

    Thanks to reply. But I am not sure with this statement:

    *", but the receive in f() of program 2 is not guaranteed to complete
    before the program exits.*"

    The MM doc says :
    A receive from an unbuffered channel happens before the send on that
    channel completes."
    I think these two statement are inconsistent.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Bryanturley at Mar 7, 2013 at 5:58 pm

    On Thursday, March 7, 2013 3:35:27 AM UTC-6, Totoro Ghibli wrote:

    Thanks to reply. But I am not sure with this statement:

    *", but the receive in f() of program 2 is not guaranteed to complete
    before the program exits.*"
    Lets say f() is running on one cpu and main() is running on another
    simultaneously.
    Which takes longer falling off the end of main() or fmt.Printf() ?

    A few other things may happen as well
    * fmt.Printf() makes at least one syscall and possibly becomes blocked
    * the os kernel might preempt the thread f() is running on just after the
    channel receive, while the thread main is running on continues to run.
    * probably more that I am not thinking of.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Jesse McNelis at Mar 7, 2013 at 9:13 am

    On Thu, Mar 7, 2013 at 6:36 PM, Tw wrote:

    code 2:

    var c = make(chan int)

    func f() {
    <-c
    print("hello, world")
    }

    func main() {
    go f()
    c <- 0
    }

    In these two cases, will the "hello world" be guaranteed to pe print?
    According to the MM doc, it only guarantees the <-c HB c <-0 vice versa.
    No guarantee. The spec doesn't say anything about scheduling of goroutines,
    or the order of non-communication actions in separate goroutines.




    --
    =====================
    http://jessta.id.au

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMar 7, '13 at 7:36a
activeMar 7, '13 at 5:58p
posts7
users5
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase