Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QReadWriteLock finding out whether it's write locked on same thread



  • I have following problem for recursive QReadWriteLock. I have it potentially locked for writing and I'd like to find out whether current thread is one that has it locked for writing. Is there way to do it?

    This is to determine whether I need to read lock it or not on current thread.
    This is professional 3D viewing software and objects are geometries (100MB+ of data is not uncommon). The code is bloated and badly designed. I cannot really have any much better management and knowledge whether it's already locked on same thread or not. Therefore recursive read-write mutex must know it.

    Any other advices how to achieve this?

    P.s.: I only want to either read lock it or don't read lock it when it's write locked (on the same thread). No other things requested like - checking in general if mutex is locked. Or wanting to write lock it when it's read locked on same thread.

    If there are no ways how to achieve this. I'll likely write read-write mutex of my own with support for this.


  • Qt Champions 2019

    I don't understand your problem completely - why does a recursive QReadWriteLock not work for you?


  • Qt Champions 2017

    @Pavel-Celba said in QReadWriteLock finding out whether it's write locked on same thread:

    I have following problem for recursive QReadWriteLock.

    Yes, the main problem is you're using recursive mutexes. Breaking ownership semantics ain't no fun.

    Is there way to do it?

    Not really no. You can tell if it's locked for writing by issuing a dummy tryLockForRead(1), but this won't tell you who locked it in the first place.

    Any other advices how to achieve this?

    Best advice - don't use recursive locking.

    If there are no ways how to achieve this. I'll likely write read-write mutex of my own with support for this.

    Smooth sailing!



  • I have decided, I'll write my own mutex meeting my requirements.


Log in to reply