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

    @JonB
    Could you be more specific?

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by
    #6

    @deisik
    Sorry, in what sense? You have said/implied you know about mutexs. Qt offers QMutex Class, you may just need lock() & unlock() methods. QMutexLocker Class is a convenience class for this. As with mutexs in general, both readers and writers need to know/have access the same mutex/locker instance, whether you achieve that via global variable, singleton or other approach.

    Shared objects need locking and unlocking around both write and read operations.

    deisikD 1 Reply Last reply
    1
    • JonBJ JonB

      @deisik
      Sorry, in what sense? You have said/implied you know about mutexs. Qt offers QMutex Class, you may just need lock() & unlock() methods. QMutexLocker Class is a convenience class for this. As with mutexs in general, both readers and writers need to know/have access the same mutex/locker instance, whether you achieve that via global variable, singleton or other approach.

      Shared objects need locking and unlocking around both write and read operations.

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

      @JonB said in Exclusive access to a variable/data structure:

      @deisik
      Sorry, in what sense? You have said/implied you know about mutexs. Qt offers QMutex Class, you may just need lock() & unlock() methods. QMutexLocker Class is a convenience class for this. As with mutexs in general, both readers and writers need to know/have access the same mutex/locker instance, whether you achieve that via global variable, singleton or other approach.

      Shared objects need locking and unlocking around both write and read operations.

      What are other approaches?

      JonBJ J.HilkJ 2 Replies Last reply
      0
      • deisikD deisik

        @JonB said in Exclusive access to a variable/data structure:

        @deisik
        Sorry, in what sense? You have said/implied you know about mutexs. Qt offers QMutex Class, you may just need lock() & unlock() methods. QMutexLocker Class is a convenience class for this. As with mutexs in general, both readers and writers need to know/have access the same mutex/locker instance, whether you achieve that via global variable, singleton or other approach.

        Shared objects need locking and unlocking around both write and read operations.

        What are other approaches?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #8

        @deisik
        Why do you want "other approaches"? You said yourself about needing a mutex, we respond confirming that, and you respond you now want something different. Why?

        If your stated goal is "Exclusive access to a variable/data structure", i.e. sharable, and across multiple threads, then what else do you have in mind? Might be different if you wanted to, e.g. send a copy via a signal, but you don't....

        deisikD 1 Reply Last reply
        0
        • deisikD deisik

          @JonB said in Exclusive access to a variable/data structure:

          @deisik
          Sorry, in what sense? You have said/implied you know about mutexs. Qt offers QMutex Class, you may just need lock() & unlock() methods. QMutexLocker Class is a convenience class for this. As with mutexs in general, both readers and writers need to know/have access the same mutex/locker instance, whether you achieve that via global variable, singleton or other approach.

          Shared objects need locking and unlocking around both write and read operations.

          What are other approaches?

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #9

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

          What are other approaches?

          prayer


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          4
          • JonBJ JonB

            @deisik
            Why do you want "other approaches"? You said yourself about needing a mutex, we respond confirming that, and you respond you now want something different. Why?

            If your stated goal is "Exclusive access to a variable/data structure", i.e. sharable, and across multiple threads, then what else do you have in mind? Might be different if you wanted to, e.g. send a copy via a signal, but you don't....

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

            @JonB said in Exclusive access to a variable/data structure:

            If your stated goal is "Exclusive access to a variable/data structure", i.e. sharable, and across multiple threads, then what else do you have in mind? Might be different if you wanted to, e.g. send a copy via a signal, but you don't....

            Basically, I need 2 things

            1. No read operation can start until the write operation is over
            2. No write operation can start until all reads are finished

            Note that I don't need to synchronize read operations as such. Multiple concurrent reads are okay and must be allowed

            I use a flag which (seemingly) enables 1. For 2, I thought about implementing a counter which gets incremented on a read operation. But then I realized that this counter itself can get corrupted on concurrent writes by data read operations

            @J-Hilk said in Exclusive access to a variable/data structure:

            prayer

            I'm already past that point

            Christian EhrlicherC M 2 Replies Last reply
            0
            • deisikD deisik

              @JonB said in Exclusive access to a variable/data structure:

              If your stated goal is "Exclusive access to a variable/data structure", i.e. sharable, and across multiple threads, then what else do you have in mind? Might be different if you wanted to, e.g. send a copy via a signal, but you don't....

              Basically, I need 2 things

              1. No read operation can start until the write operation is over
              2. No write operation can start until all reads are finished

              Note that I don't need to synchronize read operations as such. Multiple concurrent reads are okay and must be allowed

              I use a flag which (seemingly) enables 1. For 2, I thought about implementing a counter which gets incremented on a read operation. But then I realized that this counter itself can get corrupted on concurrent writes by data read operations

              @J-Hilk said in Exclusive access to a variable/data structure:

              prayer

              I'm already past that point

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

              Take a look at QReadWriteLock

              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
              4
              • deisikD deisik

                @JonB said in Exclusive access to a variable/data structure:

                If your stated goal is "Exclusive access to a variable/data structure", i.e. sharable, and across multiple threads, then what else do you have in mind? Might be different if you wanted to, e.g. send a copy via a signal, but you don't....

                Basically, I need 2 things

                1. No read operation can start until the write operation is over
                2. No write operation can start until all reads are finished

                Note that I don't need to synchronize read operations as such. Multiple concurrent reads are okay and must be allowed

                I use a flag which (seemingly) enables 1. For 2, I thought about implementing a counter which gets incremented on a read operation. But then I realized that this counter itself can get corrupted on concurrent writes by data read operations

                @J-Hilk said in Exclusive access to a variable/data structure:

                prayer

                I'm already past that point

                M Offline
                M Offline
                mpergand
                wrote on last edited by
                #12

                @deisik
                One alternative could be to use signals & slots,
                when a change occurs, a signal is emitted to the connected receivers.
                In queue mode the signal is emitted in the receiver thread (assuming the thread has an event loop).
                see here :
                https://stackoverflow.com/questions/15051553/qt-signals-queuedconnection-and-directconnection

                deisikD 1 Reply Last reply
                0
                • M mpergand

                  @deisik
                  One alternative could be to use signals & slots,
                  when a change occurs, a signal is emitted to the connected receivers.
                  In queue mode the signal is emitted in the receiver thread (assuming the thread has an event loop).
                  see here :
                  https://stackoverflow.com/questions/15051553/qt-signals-queuedconnection-and-directconnection

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

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

                  @deisik
                  One alternative could be to use signals & slots,
                  when a change occurs, a signal is emitted to the connected receivers.
                  In queue mode the signal is emitted in the receiver thread (assuming the thread has an event loop).
                  see here :
                  https://stackoverflow.com/questions/15051553/qt-signals-queuedconnection-and-directconnection

                  It would require a two-way communication, so how do you imagine it?

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    Take a look at QReadWriteLock

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

                    @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

                    JonBJ Christian EhrlicherC 2 Replies Last reply
                    0
                    • deisikD deisik has marked this topic as solved on
                    • 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

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #15

                      @deisik
                      It's the same principle as with QMutexs, which also have the try...()-type methods.

                      https://doc.qt.io/qt-6/qreadwritelock.html#details

                      In many cases, QReadWriteLock is a direct competitor to QMutex. QReadWriteLock is a good choice if there are many concurrent reads and writing occurs infrequently.

                      And, yes, as you say, don't use your own flag as accessing that will be race condition :)

                      1 Reply Last reply
                      0
                      • 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