DEV Community

Discussion on: Practical Synchronization with Go

Collapse
 
ewirch profile image
Eduard Wirch

Jolyon, your bank.go program is broken, it deadlocks: play.golang.org/p/2RE89_ER2N2

The problem is:

go func() {
  for i := 0; i < 1000; i++ {
    mu.Lock()
    defer mu.Unlock()
    bob -= 1
    alice += 1
    // mu.Unlock()
  }
}()

defer executes mu.Unlock() when the enclosing function returns. The second loop iteration will deadlock on mu.Lock().

Collapse
 
jolyon129 profile image
Jolyon

Thanks for pointing out!

Just corrected it.