Results 1 to 2 of 2

Thread: Check if QReadWriteLock is locked

  1. #1
    Join Date
    Feb 2011
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4 Qt5
    MacOS X Windows

    Default Check if QReadWriteLock is locked

    As the title suggests my question is how to check whether QReadWriteLock is locked (either for reading or writing). It seems there is no such method.

    I am thinking of the options:

    0x1) Use tryLockForWrite(0)
    The drawback is that it would require calling unlock() and, probably, have negative impact on performance. Moreover, it doesn't work when QReadWriteLock is recursive.

    0x2) Encapsulate the lock counter in the class along with QReadWriteLock. That should work in the recursive case, but it's more complex and requires additional care to protect the counter itself:
    Qt Code:
    1. template <bool recursive>
    2. class Lockable
    3. {
    4. public:
    5. void lockForRead() const
    6. {
    7. this->lock.lockForRead();
    8. this->locked.fetchAndAddRelease(1);
    9. }
    11. void lockForWrite()
    12. {
    13. this->lock.lockForWrite();
    14. this->locked.fetchAndAddRelease(1);
    15. }
    17. void unlock() const
    18. {
    19. this->lock.unlock();
    20. this->locked.fetchAndAddRelease(-1);
    21. }
    23. protected:
    24. Lockable(): lock(recursive ? QReadWriteLock::Recursive : QReadWriteLock::NonRecursive) {}
    25. Lockable(const Lockable &other) = delete;
    26. Lockable(Lockable &&other) = default;
    27. ~Lockable() //= default;
    28. {
    29. // if the lock is still not released we have a critical error
    30. //if (!lock.tryLockForWrite())
    31. if (this->locked)
    32. qFatal("Destroying lockable resource which is in use.");
    33. }
    34. private:
    35. mutable QReadWriteLock lock;
    36. mutable QAtomicInt locked = 0;
    37. }; // Lockable
    To copy to clipboard, switch view to plain text mode 

    In the end it does make me wonder why Qt has no implementation of this method? Should I request this feature?
    Magicians do not exist

  2. #2
    Join Date
    Jan 2006
    Graz, Austria
    Thanked 1,542 Times in 1,493 Posts
    Qt products
    Qt3 Qt4 Qt5
    Unix/X11 Windows

    Default Re: Check if QReadWriteLock is locked

    Information like that is so transient that you can't really make any decision on it, i.e. the actual state could change the very moment after you have read it.

    This unreliability is most likely why this is not part of the class itself, as it would incur overhead for basically no gain.


Similar Threads

  1. When use QReadWriteLock and when use QMutex?
    By Momergil in forum Qt Programming
    Replies: 2
    Last Post: 6th May 2014, 17:56
  2. QReadWriteLock locks for reading...but why?
    By Qtonimo in forum Qt Programming
    Replies: 5
    Last Post: 29th August 2012, 11:58
  3. QReadWriteLock Question
    By qtYoda in forum Newbie
    Replies: 9
    Last Post: 7th April 2011, 11:03
  4. using QReadWriteLock in QMap and Qhash
    By HERC in forum Qt Programming
    Replies: 0
    Last Post: 1st July 2010, 13:00
  5. Removing locked attribute on Mac locked files?
    By gfunk in forum Qt Programming
    Replies: 1
    Last Post: 26th October 2007, 22:40

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.