The original function does not work in the way the author intended in the
comment above the function:
// Note: x will always be unlocked upon return.
func TryUnlock(x *sync.Mutex) (success bool) {
if x.TryLock() {
// x was unlocked but is now locked by us.
x.Unlock()
return true
}
// x was unlocked to start with.
return false
}
1. if the mutex is locked, TryLock() will fail. The function will return
false, and the mutex could well still be locked (although it's also
possible that some other goroutine unlocks it in the meantime)
2. if the mutex is not locked, TryLock() will succeed. The function will
then unlock it, and the mutex could well be unlocked at function return
(although it's also possible that some other goroutine locks it in the mean
time).
Hence "x will always be unlocked upon return" is incorrect, as it's not
changed by this function. And the true/false value tells you what the value
was in the middle of execution of the function, but it could have changed
by the time the function returns.
--
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 [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/golang-nuts/f67cb076-8a00-4521-b928-cc5fbcfb8af9n%40googlegroups.com.