Hi Jason.
I see what you are saying. Ok. One idea: What if everyone else using this
mutex used only the TryLock and TryUnlock functions and proceeding only if they
returned “success”. Then they would be honoring any pre-existing, or
non-existing, lock.
This certainly isn’t what I originally had in mind, but it would be possible to
make the changes.
John
John Souvestre New Orleans LA, USA 504-454-0899
From: [email protected] <[email protected]> On Behalf Of
Jason E. Aten
Sent: 2026-01-02, Fri 21:01
To: golang-nuts <[email protected]>
Subject: [go-nuts] Re: TryUnlock() and IsLocked() functions
The main difficulty is here is akin to a use-after-check race.
After you have done an Unlock, any other goroutine could have grabbed the lock,
so returning false does not mean the lock is available.
In fact it means nothing meaningful... unless you have otherwise guaranteed no
lock contention, right? And if you've already guaranteed no contention by
another mutex, why bother with this one?
If you expand on your larger goal, we might be able to suggest
an approach that avoids TryLock.
On Friday, January 2, 2026 at 11:42:24 PM UTC-3 John Souvestre wrote:
Hi all.
I have a question about the mutexes provided by the sync library. It is
correct to say that using the 3 lines of code below, anywhere in Go code, won't
have any effect, other than to introduce a slight, non-blocking delay while
they execute? In other words, the mutex is guaranteed to be the same before
and after, and won’t hang your code, regardless of what else is going on,
including in other GoRoutines.
// Note: x is a sync.Mutex.
if x.TryLock() {
// x was unlocked but is now locked by us.
x.Unlock()
}
If so, then would these functions be non-blocking, correct, and safe to use?
// 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
}
// Note: x will be the same (locked or unlocked) upon return.
func IsLocked(x *sync.Mutex) (locked bool) {
if x.TryLock() {
// x was unlocked but is now locked by us.
x.Unlock()
return false
}
// x was locked to start with.
return true
}
I do understand that what I'm trying to achieve here is ok to do in only rare
cases. Indeed, I've only found a need for them and TryLock() on a few rare
occasions.
Thanks,
John
John Souvestre New Orleans LA, USA 504-454-0899
<tel:(504)%20454-0899>
--
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]
<mailto:[email protected]> .
To view this discussion visit
https://groups.google.com/d/msgid/golang-nuts/b0d6a801-7799-4119-b5fa-24e77db4863en%40googlegroups.com
<https://groups.google.com/d/msgid/golang-nuts/b0d6a801-7799-4119-b5fa-24e77db4863en%40googlegroups.com?utm_medium=email&utm_source=footer>
.
--
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/002101dc7c6f%24e7210ff0%24b5632fd0%24%40Souvestre.com.