Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QReadLocker unlock question
Forum Updated to NodeBB v4.3 + New Features

QReadLocker unlock question

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 2 Posters 495 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.
  • T Offline
    T Offline
    touusen
    wrote on last edited by touusen
    #1

    I have some code like this, but I do not know if readLocker.unlock(); is safe or not.
    thanks for advice.

    QReadLocker readLocker(&locker);
    
    Login login(name, password);
    if (!login.isLoggedIn()) {
        return false;
    }
    
    readLocker.unlock();   
    onUserChanged();      //signal
    
    JonBJ 1 Reply Last reply
    0
    • T touusen

      I have some code like this, but I do not know if readLocker.unlock(); is safe or not.
      thanks for advice.

      QReadLocker readLocker(&locker);
      
      Login login(name, password);
      if (!login.isLoggedIn()) {
          return false;
      }
      
      readLocker.unlock();   
      onUserChanged();      //signal
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @touusen
      In what sense " is safe or not"? Your code looks fine to me (assuming what you want to protect is just the Login object and the isLoggedIn() call).

      T 1 Reply Last reply
      0
      • JonBJ JonB

        @touusen
        In what sense " is safe or not"? Your code looks fine to me (assuming what you want to protect is just the Login object and the isLoggedIn() call).

        T Offline
        T Offline
        touusen
        wrote on last edited by
        #3

        @JonB
        Thank you.
        I mean , the locker will be unlocked when the QMutexLocker is destroyed,
        but I used readLocker.unlock() to unlock the locker manually and I have no idea about the status of locker when QMutexLocker is destroyed.

        JonBJ 1 Reply Last reply
        0
        • T touusen

          @JonB
          Thank you.
          I mean , the locker will be unlocked when the QMutexLocker is destroyed,
          but I used readLocker.unlock() to unlock the locker manually and I have no idea about the status of locker when QMutexLocker is destroyed.

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

          @touusen
          I'm still not understanding your doubt. QReadLocker will release the lock when it goes out of scope or explicitly if you call QReadLocker::unlock().

          T 1 Reply Last reply
          0
          • JonBJ JonB

            @touusen
            I'm still not understanding your doubt. QReadLocker will release the lock when it goes out of scope or explicitly if you call QReadLocker::unlock().

            T Offline
            T Offline
            touusen
            wrote on last edited by
            #5

            @JonB
            In my code, does QReadLocker release the lock twice?
            (① readLocker.unlock(); ②out of scope)

            JonBJ 1 Reply Last reply
            0
            • T touusen

              @JonB
              In my code, does QReadLocker release the lock twice?
              (① readLocker.unlock(); ②out of scope)

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

              @touusen

              • QReadLocker::unlock() call will unlock/release the lock.
              • QReadLocker out-of-scope destructor will unlock/release the lock if it holds a lock. You should assume if it has already unlocked/released it will not do it a second time.

              Did you actually try your code? Since you seem to be so worried about it, and are not comfortable with answers, why don't you do so to reassure yourself at this point?

              T 1 Reply Last reply
              0
              • T touusen has marked this topic as solved on
              • JonBJ JonB

                @touusen

                • QReadLocker::unlock() call will unlock/release the lock.
                • QReadLocker out-of-scope destructor will unlock/release the lock if it holds a lock. You should assume if it has already unlocked/released it will not do it a second time.

                Did you actually try your code? Since you seem to be so worried about it, and are not comfortable with answers, why don't you do so to reassure yourself at this point?

                T Offline
                T Offline
                touusen
                wrote on last edited by
                #7

                @JonB
                Thank you very much.

                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