As Jason said it might be easier to go up a level and describe the problem you’re trying to solve. It is trivial to use a mutex to coordinate safely so it’s a bit unclear why you are complicating it with another layer. If you are trying to have things not block that is trivial too. 

Your basic problem is you are trying to ask is access available then get access - which is inherently racy. Breyer to just request access, and return try or false if it was granted - using TryLock and if true use defer to schedule the unlock. 

On Jan 2, 2026, at 11:15 PM, 'John Souvestre' via golang-nuts <[email protected]> wrote:



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:24PM 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

 

--
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/b0d6a801-7799-4119-b5fa-24e77db4863en%40googlegroups.com.

--
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.

--
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/33961CA4-DCCB-4CA6-BE70-DD40BF6392F8%40me.com.

Reply via email to