The standard library TryLock() gives correct answers. Your code which
returns true or false, only tells you whether the mutex happened to be
locked or not locked at some instant in the past - which is generally not
very useful.
Are you trying to measure how much lock contention is going on? Then
perhaps you could do something like this:
if x.TryLock() {
fmt.Println("No contention")
} else {
fmt.Println("Contention!")
x.Lock() // this blocks until the lock is obtained (of
course it might already be free before this call)
} // either way, we now have the mutex locked
defer x.Unlock()
do_something()
return
More sensibly, you might have two sync.atomic counters, and increment one
or the other depending on which branch is taken.
--
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/d59acd47-8ffe-4f4a-b11d-b895a6da92bfn%40googlegroups.com.