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. Exclusive (write) access to a variable/data structure
Forum Updated to NodeBB v4.3 + New Features

Exclusive (write) access to a variable/data structure

Scheduled Pinned Locked Moved Solved General and Desktop
25 Posts 7 Posters 2.5k Views 2 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.
  • deisikD deisik

    @Christian-Ehrlicher said in Exclusive (write) access to a variable/data structure:

    Take a look at QReadWriteLock

    That seems to be the thing

    tryLockForRead() and tryLockForWrite() are the functions that I need

    Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #16

    @deisik said in Exclusive (write) access to a variable/data structure:

    tryLockForRead() and tryLockForWrite() are the functions that I need

    You really better should use QReadLocker and QWriteLocker for this task.

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    deisikD 1 Reply Last reply
    2
    • Christian EhrlicherC Christian Ehrlicher

      @deisik said in Exclusive (write) access to a variable/data structure:

      tryLockForRead() and tryLockForWrite() are the functions that I need

      You really better should use QReadLocker and QWriteLocker for this task.

      deisikD Offline
      deisikD Offline
      deisik
      wrote on last edited by
      #17

      @Christian-Ehrlicher

      How much overhead does lockForRead() incur (in terms of CPU cycles)?

      Christian EhrlicherC 1 Reply Last reply
      0
      • deisikD deisik

        @Christian-Ehrlicher

        How much overhead does lockForRead() incur (in terms of CPU cycles)?

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #18

        @deisik It depends on your cpu, compiler and optimization level. Why is this important? You need a locking mechanism so use one. If you have problems with the speed later on then do profiling.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        deisikD 1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          @deisik It depends on your cpu, compiler and optimization level. Why is this important? You need a locking mechanism so use one. If you have problems with the speed later on then do profiling.

          deisikD Offline
          deisikD Offline
          deisik
          wrote on last edited by deisik
          #19

          @Christian-Ehrlicher

          You see, I need this feature only in very specific cases, but the data structure for which I need it is used application-wide. So if I could just inherit from QReadWriteLock for this class and make calls to its internal data tree lock-aware, it would greatly simplify things

          Christian EhrlicherC 1 Reply Last reply
          0
          • deisikD deisik

            @Christian-Ehrlicher

            You see, I need this feature only in very specific cases, but the data structure for which I need it is used application-wide. So if I could just inherit from QReadWriteLock for this class and make calls to its internal data tree lock-aware, it would greatly simplify things

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #20

            @deisik said in Exclusive (write) access to a variable/data structure:

            So if I could just inherit from QReadWriteLock f

            Why inherit from this? There is no need for this...

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            deisikD 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              @deisik said in Exclusive (write) access to a variable/data structure:

              So if I could just inherit from QReadWriteLock f

              Why inherit from this? There is no need for this...

              deisikD Offline
              deisikD Offline
              deisik
              wrote on last edited by deisik
              #21

              @Christian-Ehrlicher said in Exclusive (write) access to a variable/data structure:

              @deisik said in Exclusive (write) access to a variable/data structure:

              So if I could just inherit from QReadWriteLock f

              Why inherit from this? There is no need for this...

              Why not if it doesn't incur significant overhead? It feels like a logical choice to me. You just lock/unlock a data object when you need without overthinking it (this is not to say you shouldn't think it over)

              Christian EhrlicherC 1 Reply Last reply
              0
              • deisikD deisik

                @Christian-Ehrlicher said in Exclusive (write) access to a variable/data structure:

                @deisik said in Exclusive (write) access to a variable/data structure:

                So if I could just inherit from QReadWriteLock f

                Why inherit from this? There is no need for this...

                Why not if it doesn't incur significant overhead? It feels like a logical choice to me. You just lock/unlock a data object when you need without overthinking it (this is not to say you shouldn't think it over)

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #22

                This class is not meant to be derived from... use it as shown in the documentation.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                deisikD 1 Reply Last reply
                1
                • Christian EhrlicherC Christian Ehrlicher

                  This class is not meant to be derived from... use it as shown in the documentation.

                  deisikD Offline
                  deisikD Offline
                  deisik
                  wrote on last edited by deisik
                  #23

                  @Christian-Ehrlicher

                  Okay, I implemented both approaches (by inheriting QReadWriteLock and by using a local QReadWriteLock object). I didn't notice any difference – both implementations are working just fine, and block access as designed

                  But personally, I'm going to stick with subclassing QReadWriteLock as it feels like a lot more authentic way of doing things in C++, and you don't need to bother about helper functions (you can lock the entire data object directly) while not having to handle a separate QReadWriteLock object within the class itself

                  SGaistS 1 Reply Last reply
                  0
                  • deisikD deisik

                    @Christian-Ehrlicher

                    Okay, I implemented both approaches (by inheriting QReadWriteLock and by using a local QReadWriteLock object). I didn't notice any difference – both implementations are working just fine, and block access as designed

                    But personally, I'm going to stick with subclassing QReadWriteLock as it feels like a lot more authentic way of doing things in C++, and you don't need to bother about helper functions (you can lock the entire data object directly) while not having to handle a separate QReadWriteLock object within the class itself

                    SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #24

                    @deisik composition is a core part of C++ and is encouraged over inheritance. You usually inherit when you want to augment the base class with new capabilities. In your case, you are leaking implementation details by doing so. If you need to lock the access to the whole data structure, then maybe you were putting the original lock in the wrong place.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    deisikD 1 Reply Last reply
                    1
                    • SGaistS SGaist

                      @deisik composition is a core part of C++ and is encouraged over inheritance. You usually inherit when you want to augment the base class with new capabilities. In your case, you are leaking implementation details by doing so. If you need to lock the access to the whole data structure, then maybe you were putting the original lock in the wrong place.

                      deisikD Offline
                      deisikD Offline
                      deisik
                      wrote on last edited by deisik
                      #25

                      @SGaist said in Exclusive (write) access to a variable/data structure:

                      @deisik composition is a core part of C++ and is encouraged over inheritance

                      Then why care about C++ at all?

                      You usually inherit when you want to augment the base class with new capabilities. In your case, you are leaking implementation details by doing so

                      Leaking to whom or what, and why is it inherently a bad thing?

                      If you need to lock the access to the whole data structure, then maybe you were putting the original lock in the wrong place

                      Yes, when I need to dramatically change its contents. When I need to change/retrieve from another thread a single element in a list, I now use QReadWriteLock-enabled versions of regular getter/setter functions

                      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