Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Using QReadWriteLock
Forum Updated to NodeBB v4.3 + New Features

Using QReadWriteLock

Scheduled Pinned Locked Moved General and Desktop
9 Posts 7 Posters 5.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hansg
    wrote on 3 Feb 2011, 00:26 last edited by
    #1

    Hi,

    The documentation for QReadWriteLock says "Note that the lock type cannot be changed when trying to lock recursively, i.e. it is not possible to lock for reading in a thread that already has locked for writing (and vice versa)."

    I believe it makes perfect sense that the QReadWriteLock should not allow a recursive lock to change the lock type from reading to writing (to prevent deadlocks), but I don't see why the class should prevent my thread from obtaining a read lock if the current thread already has a write lock.

    I would like to be able to write a function that obtains a write lock, and then enter another function that obtains a read lock, without unlocking the write lock.

    Is there a way to do this without modifying the QReadWriteLock class?

    Thanks,
    Hans

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tbscope
      wrote on 3 Feb 2011, 05:25 last edited by
      #2

      Use two QReadWriteLock objects?
      One for writing (or a QWriteLocker) and one for reading (or a QReadLocker)?

      1 Reply Last reply
      0
      • G Offline
        G Offline
        giesbert
        wrote on 3 Feb 2011, 05:25 last edited by
        #3

        It will not wok, as a write lock is an exclusive lock:

        • one write lock or multiple read locks.

        Having a write lock and requiring a readlock in a sub function must create a deadlock.
        If you already have a write lock, you have an exclusive lock, use it.

        Nokia Certified Qt Specialist.
        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tobias.hunger
          wrote on 3 Feb 2011, 08:29 last edited by
          #4

          tbscope: If somebody has a write lock you will not want anybody else to have a read lock, so splitting up the readwrite lock into two separate locks does not really make sense.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on 3 Feb 2011, 13:33 last edited by
            #5

            What is wrong about requesting a read lock on the very same thread that already has a write lock? Shouldn't that be allowed? Requesting a read-write lock succeeds, thus granting me implicit read access. Am I missing something?

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • T Offline
              T Offline
              tobias.hunger
              wrote on 3 Feb 2011, 13:38 last edited by
              #6

              Well, if you hold the write lock, then no requesting a read lock will block. I am sure logic to make ignore the read lock request could get added, but that introduces additional overhead that is almost always unnecessary.

              Considering that locking is often in the critical path you do not want that overhead.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                samapico
                wrote on 30 Apr 2012, 23:23 last edited by
                #7

                I know this thread is pretty old, but my issue is exactly as described by OP...

                I had a class that was being used by multiple threads, and I needed to make it thread-safe... It has relatively lots of reads compared to writes, so I thought a QReadWriteLock would be great.

                I went through all my (public) methods, and added in a QReadLocker(&rwLock) or QWriteLocker(&rwLock), depending if the method was const or not...
                It certainly doesn't produce the most effective code, but it guarantees thread safety and allows multiple concurrent reads...

                But just like Volker, several methods that require a write lock will also call some public methods that are read-only that request a read lock.
                Is the only way to make this correctly to make all public methods simply lock and call a private method like this? :

                @void DoSomething()
                {
                QWriteLock lock(&rwLock);
                DoSomethingInternal(); //<-- This method doesn't lock
                }@

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  MuldeR
                  wrote on 1 May 2012, 00:18 last edited by
                  #8

                  [quote author="samapico" date="1335828210"]Is the only way to make this correctly to make all public methods simply lock and call a private method like this? :

                  @void DoSomething()
                  {
                  QWriteLock lock(&rwLock);
                  DoSomethingInternal(); //<-- This method doesn't lock
                  }@
                  [/quote]

                  That would have been my first idea too.

                  Another idea would be using a [[doc:QThreadStorage]] to store a "hasWriteLock" flag for each thread.

                  Then something like this should be possible:

                  @QThreadStorage<bool> hasWriteLock;

                  void DoWriteAction()
                  {
                  QWriteLock lock(&rwLock);
                  hasWriteLock.setLocalData(true)

                  /* Do stuff */

                  DoReadAction();

                  /* Do stuff */

                  hasWriteLock.setLocalData(false)
                  }

                  void DoReadAction()
                  {
                  const bool needLock = (!(hasWriteLock.hasLocalData() && hasWriteLock.localData()));

                  if(needLock) rwLock.lockForRead();

                  /* Do stuff */

                  if(needLock) rwLock.unlock();
                  }@

                  My OpenSource software at: http://muldersoft.com/

                  Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                  Go visit the coop: http://youtu.be/Jay...

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    samapico
                    wrote on 1 May 2012, 18:42 last edited by
                    #9

                    Hmmm, not all my threads are QThreads... I'm guessing QThreadStorage wouldn't work right for me? In fact, only the UI stuff runs in QThreads in my project, I think.

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved