4

I'm giving a presentation about the Go Memory Model. The memory model says that without a happens-before relationship between a write in one goroutine, and a read in another goroutine, there is no guarantee that the reader will observe the change.

To have a bigger impact on the audience, instead of just telling them that bad things can happen if you don't synchronize, I'd like to show them.

When I run the below code on my machine (2017 MacBook Pro with 3.5GHz dual-core Intel Core i7), it exits successfully.

Is there anything I can do to demonstrate the memory visibility issues?

For example are there any specific changes to the following values I could make to demonstrate the issue:

  • use different compiler settings
  • use an older version of Go
  • run on a different operating system
  • run on different hardware (such as ARM or a machine with multiple NUMA nodes).

For example in Java the flags -server and -client affect the optimizations the JVM takes and lead to visibility issues occurring.

I'm aware that the answer may be no, and that the spec may have been written to give future maintainers more flexibility with optimization. I'm aware I can make the code never exit by setting GOMAXPROCS=1 but that doesn't demonstrate visibility issues.

package main
var a string
var done bool

func setup() {
    a = "hello, world"
    done = true
}

func main() {
    go setup()
    for !done {
    }
    print(a)
}
8
  • 4
    How about using the race detector? It reports an error on the above program. Commented Jan 20, 2019 at 23:49
  • Code doesn't finish on playground play.golang.org Commented Jan 20, 2019 at 23:55
  • 2
    @Uvelichitel That's because the Playground has GOMAXPROCS=1, so the for loop will never yield and the other goroutine won't run. This is a problem but is separate from memory visibility problems. If I set GOMAXPROCS=2 it will return: play.golang.org/p/0uImutl7UPT Commented Jan 21, 2019 at 0:09
  • 7
    One of the impediments to reliably reproducing observable reorderings is that Intel processors generally implement a stronger memory model than is required by the language. (This is certainly true in Java.) Which means if your systems are Linux, Windows, or Mac, you are probably already starting with one hand tied behind your back. But your question belies a wrong way of thinking. Yes, users want to be shown "what breaks." But such failures are generally rare probablistic events, not deterministic failures. And said users are thinking deterministically. Teach them to think otherwise. Commented Jan 21, 2019 at 1:20
  • 2
    "Is there anything I can do to demonstrate the memory visibility issues?" Not much. But ask yourself: If someone is interested in the memory model of a language then he/she is probably senior enough to know that race conditions are a 100% no go even if given a certain CPU and a certain workload the race might not manifest. Commented Jan 21, 2019 at 5:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.