I use QReadWriteLock in my application to protect access to the resource object.
I use QReadLocks and QWriteLocks where I can, but sometimes I need a "long-lived" lock that crosses the boundaries of functions. Therefore, sometimes I need to be able to query the status of QReadWriteLock (or QMutex, the same in this situation), because unlocking a mutex that is already unlocked is very bad.
For QMutex:
Disabling a mutex that is not locked results in undefined behavior.
For QReadWriteLock:
Trying to unlock a lock that is not locked is an error and will end the program.
Being able to query the state of a mutex is also very useful for debugging statements to verify that before I start some operation, I really have this “durable” lock.
But I really want to be able to unlock the mutex if I have a lock, and not unlock it if I don't have it. It will make my day. The IMO function unlock()should already be doing this for me, but it is not.
How to get around this?
(and no, using the mode Recursivein this situation does not help)
Lucas source
share