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.1k 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.
  • D Offline
    D Offline
    deisik
    wrote on 11 Oct 2023, 11:29 last edited by deisik 10 Nov 2023, 13:43
    #1

    I have a few threads which are accessing the same data structure simultaneously, with only one changing it. However, I need to prevent other threads from reading it while it is being changed (for pretty obvious reasons). So the question is essentially about mutexes. Right now, I'm using a special flag (semaphore) in that data structure, which is raised when the data is about to be modified. But it doesn't feel like a very good solution overall, nor does it seem like a really fail-safe way of granting exclusive write access

    Then what is the most elegant way of doing that (without using global variables)?

    J 1 Reply Last reply 11 Oct 2023, 11:35
    0
    • D deisik
      11 Oct 2023, 13:07

      @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

      C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 11 Oct 2023, 13:39 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

      D 1 Reply Last reply 11 Oct 2023, 14:17
      4
      • D deisik
        11 Oct 2023, 11:29

        I have a few threads which are accessing the same data structure simultaneously, with only one changing it. However, I need to prevent other threads from reading it while it is being changed (for pretty obvious reasons). So the question is essentially about mutexes. Right now, I'm using a special flag (semaphore) in that data structure, which is raised when the data is about to be modified. But it doesn't feel like a very good solution overall, nor does it seem like a really fail-safe way of granting exclusive write access

        Then what is the most elegant way of doing that (without using global variables)?

        J Offline
        J Offline
        JonB
        wrote on 11 Oct 2023, 11:35 last edited by
        #2

        @deisik
        As described and as you say, use a mutex.

        D 1 Reply Last reply 11 Oct 2023, 11:37
        0
        • J JonB
          11 Oct 2023, 11:35

          @deisik
          As described and as you say, use a mutex.

          D Offline
          D Offline
          deisik
          wrote on 11 Oct 2023, 11:37 last edited by
          #3

          @JonB
          Could you be more specific?

          O J 2 Replies Last reply 11 Oct 2023, 11:38
          0
          • D deisik
            11 Oct 2023, 11:37

            @JonB
            Could you be more specific?

            O Offline
            O Offline
            ollarch
            wrote on 11 Oct 2023, 11:38 last edited by
            #4

            @deisik As @JonB said, use a Mutex. Lock it before accessing to data and unlock it after the access.

            D 1 Reply Last reply 11 Oct 2023, 11:40
            0
            • O ollarch
              11 Oct 2023, 11:38

              @deisik As @JonB said, use a Mutex. Lock it before accessing to data and unlock it after the access.

              D Offline
              D Offline
              deisik
              wrote on 11 Oct 2023, 11:40 last edited by
              #5

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

              @deisik As @JonB said, use a Mutex. Lock it before accessing to data and unlock it after the access.

              What mutex?

              1 Reply Last reply
              0
              • D deisik
                11 Oct 2023, 11:37

                @JonB
                Could you be more specific?

                J Offline
                J Offline
                JonB
                wrote on 11 Oct 2023, 11:43 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.

                D 1 Reply Last reply 11 Oct 2023, 11:44
                1
                • J JonB
                  11 Oct 2023, 11:43

                  @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.

                  D Offline
                  D Offline
                  deisik
                  wrote on 11 Oct 2023, 11:44 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?

                  J J 2 Replies Last reply 11 Oct 2023, 11:49
                  0
                  • D deisik
                    11 Oct 2023, 11:44

                    @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 Offline
                    J Offline
                    JonB
                    wrote on 11 Oct 2023, 11:49 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....

                    D 1 Reply Last reply 11 Oct 2023, 13:07
                    0
                    • D deisik
                      11 Oct 2023, 11:44

                      @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 Offline
                      J Offline
                      J.Hilk
                      Moderators
                      wrote on 11 Oct 2023, 12:01 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
                      • J JonB
                        11 Oct 2023, 11:49

                        @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....

                        D Offline
                        D Offline
                        deisik
                        wrote on 11 Oct 2023, 13:07 last edited by deisik 10 Nov 2023, 13:35
                        #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

                        C M 2 Replies Last reply 11 Oct 2023, 13:39
                        0
                        • D deisik
                          11 Oct 2023, 13:07

                          @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

                          C Offline
                          C Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on 11 Oct 2023, 13:39 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

                          D 1 Reply Last reply 11 Oct 2023, 14:17
                          4
                          • D deisik
                            11 Oct 2023, 13:07

                            @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 11 Oct 2023, 13:44 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

                            D 1 Reply Last reply 11 Oct 2023, 14:16
                            0
                            • M mpergand
                              11 Oct 2023, 13:44

                              @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

                              D Offline
                              D Offline
                              deisik
                              wrote on 11 Oct 2023, 14:16 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
                              • C Christian Ehrlicher
                                11 Oct 2023, 13:39

                                Take a look at QReadWriteLock

                                D Offline
                                D Offline
                                deisik
                                wrote on 11 Oct 2023, 14:17 last edited by deisik 10 Nov 2023, 14:30
                                #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

                                J C 2 Replies Last reply 11 Oct 2023, 14:34
                                0
                                • D deisik has marked this topic as solved on 11 Oct 2023, 14:30
                                • D deisik
                                  11 Oct 2023, 14:17

                                  @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

                                  J Offline
                                  J Offline
                                  JonB
                                  wrote on 11 Oct 2023, 14:34 last edited by JonB 10 Nov 2023, 14:36
                                  #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
                                  • D deisik
                                    11 Oct 2023, 14:17

                                    @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

                                    C Offline
                                    C Offline
                                    Christian Ehrlicher
                                    Lifetime Qt Champion
                                    wrote on 11 Oct 2023, 14:57 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

                                    D 1 Reply Last reply 11 Oct 2023, 15:47
                                    2
                                    • C Christian Ehrlicher
                                      11 Oct 2023, 14:57

                                      @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.

                                      D Offline
                                      D Offline
                                      deisik
                                      wrote on 11 Oct 2023, 15:47 last edited by
                                      #17

                                      @Christian-Ehrlicher

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

                                      C 1 Reply Last reply 11 Oct 2023, 15:51
                                      0
                                      • D deisik
                                        11 Oct 2023, 15:47

                                        @Christian-Ehrlicher

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

                                        C Offline
                                        C Offline
                                        Christian Ehrlicher
                                        Lifetime Qt Champion
                                        wrote on 11 Oct 2023, 15:51 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

                                        D 1 Reply Last reply 11 Oct 2023, 16:03
                                        0
                                        • C Christian Ehrlicher
                                          11 Oct 2023, 15:51

                                          @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.

                                          D Offline
                                          D Offline
                                          deisik
                                          wrote on 11 Oct 2023, 16:03 last edited by deisik 10 Nov 2023, 16:03
                                          #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

                                          C 1 Reply Last reply 11 Oct 2023, 16:04
                                          0
                                          • D deisik
                                            11 Oct 2023, 16:03

                                            @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

                                            C Offline
                                            C Offline
                                            Christian Ehrlicher
                                            Lifetime Qt Champion
                                            wrote on 11 Oct 2023, 16:04 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

                                            D 1 Reply Last reply 11 Oct 2023, 16:05
                                            0

                                            1/25

                                            11 Oct 2023, 11:29

                                            • Login

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