Hi,
I'm using a QReadWriteLock in my application to guard access to a resource object.
I'm using QReadLocks and QWriteLocks where I can, but sometimes I need a "long-lived" lock that crosses function boundaries. So sometimes I need to be able to query the state of the QReadWriteLock (or QMutex, same thing in this situation) because unlocking a mutex that is already unlocked is very bad.
For QMutex:
Unlocking a mutex that is not locked results in undefined behavior.
For QReadWriteLock:
Attempting to unlock a lock that is not locked is an error, and will result in program termination.
Being able to query the state of the mutex would also come in very handy for debug assertions to test that before starting some operation, I really do have that "long-lived" lock.
But all I really want is to be able to unlock a mutex if I have the lock, and not unlock it if I don't have it. That would make my day. IMO the unlock()
function should already do this for me, but it doesn't.
Any way around this?
(and no, using Recursive
mode does not help in this situation)