Derive happens-before and DRF-SC
Derive happens-before and DRF-SC
Happens-before is the transitive closure of sequenced-before and synchronized-before: if A precedes B and B precedes C in that relation, A precedes C. A data race is a conflicting pair of accesses to the same location, at least one a write and at least one non-synchronizing, not ordered by happens-before. Race-free programs have outcomes explainable by a sequentially consistent interleaving (DRF-SC).
package main
import ( "fmt")
func main() { var x int a := make(chan struct{}) b := make(chan struct{}) go func() { x = 12; close(a) }() go func() { <-a; close(b) }() <-b fmt.Println(x)}Trace write → close(a) → receive(a) → close(b) → receive(b) → read. The middle goroutine need not read x to carry the ordering. No sleep is required.
Expected output:
12Race-free does not mean deterministic ordering
package main
import ( "fmt")
func main() { c := make(chan int, 2) go func() { c <- 1 }() go func() { c <- 2 }() a, b := <-c, <-c fmt.Println(a + b)}Both send orders are allowed, and either receiver order yields sum 3. DRF-SC does not guarantee fairness or one fixed interleaving. Two ordinary reads alone do not conflict; writes require ordering relative to conflicting accesses.
Expected output:
3Try it locally
Build a two-hop close signal publishing 23 to main.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Memory model 02: Derive happens-before and DRF-SC
Check the lesson objective before moving on.
If A happens-before B and B happens-before C, what follows?
What does DRF-SC guarantee?