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. What QReadLocker guarantee?
Forum Updated to NodeBB v4.3 + New Features

What QReadLocker guarantee?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 4.0k 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.
  • F Offline
    F Offline
    feedmeqt
    wrote on last edited by
    #1

    Hi,

    I've a global static array and two threads use the data from the array for purposes. One thread(threadA) just find the array index(QHash<>) and update the data, whereas other thread(threadB) just read the same array.Since, threadA gets data from network which is not predictable and threadB which basically polls the data at some interval, but done outside the application.

    I'm using QReadLocker(threadB) and QwriteLocker(threadA).So, my doubt is like, If threadA is doing some update operation on the global array, will the threadB get block or get inconsistent data during polling.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      The sole purpose of QReadLocker, QWriteLocker and QReadWriteLocker is to synchronize read and write access. As long as a write lock is active, read attempts are blocked. As long as read blocks are active, a write attempt is blocked.

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

      1 Reply Last reply
      0
      • F Offline
        F Offline
        feedmeqt
        wrote on last edited by
        #3

        Well, then what is the difference in using Qmutex and QReadLocker, QWriteLocker , QReadWriteLocker. Is that the name which can remove the confusion from developer who is using it. Like, QReadLocker is associated with read and QWriteLocker associate with write. Rather than giving our own name for QMutex(eg QMutex readMutex.. etc).

        So, plz tell me which mechanism can I use for the above scenario,

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          The difference is, that a QReadLocker will not block another QReadLocker.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            To cite the docs of QReadWriteLock:

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

            bq. Qt 4 also introduces a new synchronization class: QReadWriteLock. It is similar to QMutex, except that it distinguishes between "read" and "write" access to shared data and allows multiple readers to access the data simultaneously. Using QReadWriteLock instead of QMutex when it is possible can make multithreaded programs more concurrent.

            So, while QMutex ensures that only one single thread can access the data at a given time, regardless of read or write access, the QReadWriteLock and friends grant unlimited read access but ensure that only one single thread can write the data at a given time and block all read attempts during this time.

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

            1 Reply Last reply
            0
            • Z Offline
              Z Offline
              ZapB
              wrote on last edited by
              #6

              As Andre and Volker said, the Q{Read|Write|ReadWriter}Lock classes and their (*Locker classes which you should use) allow more fine-grained control over your locks to minimise lock contention and therefore hopefully let you application be more concurrent.

              Nokia Certified Qt Specialist
              Interested in hearing about Qt related work

              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