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.