QReadLocker unlock question
-
I have some code like this, but I do not know if
readLocker.unlock();is safe or not.
thanks for advice.QReadLocker readLocker(&locker); Login login(name, password); if (!login.isLoggedIn()) { return false; } readLocker.unlock(); onUserChanged(); //signal -
I have some code like this, but I do not know if
readLocker.unlock();is safe or not.
thanks for advice.QReadLocker readLocker(&locker); Login login(name, password); if (!login.isLoggedIn()) { return false; } readLocker.unlock(); onUserChanged(); //signal -
@touusen
In what sense " is safe or not"? Your code looks fine to me (assuming what you want to protect is just theLoginobject and theisLoggedIn()call). -
@JonB
Thank you.
I mean , the locker will be unlocked when the QMutexLocker is destroyed,
but I usedreadLocker.unlock()to unlock the locker manually and I have no idea about the status of locker when QMutexLocker is destroyed. -
@touusen
I'm still not understanding your doubt.QReadLockerwill release the lock when it goes out of scope or explicitly if you callQReadLocker::unlock(). -
@JonB
In my code, doesQReadLockerrelease the lock twice?
(①readLocker.unlock();②out of scope)QReadLocker::unlock()call will unlock/release the lock.QReadLockerout-of-scope destructor will unlock/release the lock if it holds a lock. You should assume if it has already unlocked/released it will not do it a second time.
Did you actually try your code? Since you seem to be so worried about it, and are not comfortable with answers, why don't you do so to reassure yourself at this point?
-
T touusen has marked this topic as solved on
-
QReadLocker::unlock()call will unlock/release the lock.QReadLockerout-of-scope destructor will unlock/release the lock if it holds a lock. You should assume if it has already unlocked/released it will not do it a second time.
Did you actually try your code? Since you seem to be so worried about it, and are not comfortable with answers, why don't you do so to reassure yourself at this point?