Is it safe to unlock a mutex twice? My code:
var m sync.RWMutex = sync.RWMutex{}
func Read() {
m.RLock()
defer m.RUnlock()
// Do something that needs lock
err := SomeFunction1()
if err != nil {
return
}
m.RUnlock()
// Do something that does not need lock
SomeFunction2()
}
I need defer m.RUnlock()
for the case SomeFunction1()
returns error. But when SomeFunction1()
returns without error, m
will be unlocked twice by m.RUnlock()
and defer m.RUnlock()
.
Is it safe to unlock the mutex twice? If not, how should I fix my code?