You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I don't see how this can be used safely by multiple goroutines. TryLock is safe because if it succeeds it locks the mutex. But in your example in between IsLocked returning and the call to Unlock some other goroutine may call Unlock.
I don't see how this can be used safely by multiple goroutines. TryLock is safe because if it succeeds it locks the mutex. But in your example in between IsLocked returning and the call to Unlock some other goroutine may call Unlock.
Maybe add TryUnlock?
I think it's possible.
IsLocked use atomic.Load to check whether it's locked.
If it's locked, try unlock or avoid locking it again.
What actually would be the use case for attempted unlocking? I'm wondering what this could be but have no idea where this is needed not just because the underlying code is fundamentally broken?
Locks are typically used to ensure exclusive access to some resource.
The need for something like IsLocked or TryUnlock would indicate that the code at that point does not know if it's locked, which runs counter to proper use of locks, and that exclusivity isn't being preserved.
Some time, if a developer forgot to unlock and try to lock the Mutex, it will cause dead lock.
And go 1.18 add TryLock().
But this is no for Unlock().
Still, if if a developer forgot to lock and try to unlock the Mutex, it will panic.
Why not add a IsLocked().
The text was updated successfully, but these errors were encountered: