Ugh. It makes sense now. cond.L.Lock() gets called from the first goroutine, then immediately followed by another Lock() from the second. Therefore, in order to properly implement Lock(), it actually has to be blocking.
On Monday, March 13, 2017 at 4:35:04 PM UTC-7, dja...@gmail.com wrote: > > > > On Tuesday, March 14, 2017 at 12:10:31 AM UTC+2, Albert Tedja wrote: >> >> I am writing a custom Locker which does a check if Lock() is being called >> more than once and throws a panic if it does. >> I tested it to see if I can use it with sync.Cond, and for the most part, >> it was running as intended, except once in a while >> the panic check triggered. >> >> Here's the dumbed down version of what I am trying to do: >> >> https://play.golang.org/p/2tWqY221RM >> >> I cannot get it to reproduce on the Go Playground, but copy paste it to >> your local computer and see if you get it to panic. >> Here's what I got: >> >> panic: Cannot lock twice >> goroutine 5 [running]: >> main.(*CustomLocker).Lock(0xc42000e2e0) >> /.../go/src/github.com/test/custom_locker.go:18 +0xc9 >> main.main.func1(0xc4200142c0, 0xc42000e2f0, 0xc42000e2d0) >> /.../go/src/github.com/test/custom_locker.go:42 +0x37 >> created by main.main >> /.../go/src/github.com/test/custom_locker.go:50 +0x159 >> exit status 2 >> >> I thought sync.Cond.Wait() should Unlock(), sleeps itself, then when >> awaken by Broadcast() try to Lock() again? >> > > Hi, > > First, your custom Locker implements Locker interface, but does not lock > as it call Unlock at function return. > Second, Locker main job is to call Lock() multiple times (from different > goroutines) > > Djadala > -- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.