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().
defer
mu.Unlock()
mu.Lock()
Thanks for pointing out!
Just corrected it.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Jolyon, your bank.go program is broken, it deadlocks: play.golang.org/p/2RE89_ER2N2
The problem is:
defer
executesmu.Unlock()
when the enclosing function returns. The second loop iteration will deadlock onmu.Lock()
.Thanks for pointing out!
Just corrected it.