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. QMutexLocker and return value [SOLVED]

QMutexLocker and return value [SOLVED]

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

    Hi,

    I have a general question about QMutexLocker. What happens when I leave a function returning a "protected" value.

    I usually use QMutex as follows, where m_value is the protected value:
    @int getValue()
    {
    m_mutex.lock();
    int v = m_value;
    m_mutex.unlock();

    return v;
    }
    @

    Now with QMutexLocker:
    @int getValue()
    {
    QMutexLocker( m_mutex );

    return m_value;
    }@

    But what happens when m_value is returned? Is m_value returned before QMutexLocker is destroyed or vice versa? Is m_value still safe?

    regards
    Oliver

    1 Reply Last reply
    0
    • T Offline
      T Offline
      topse
      wrote on last edited by
      #2

      Maybe like that:

      @
      int getValue()
      {
      QMutexLocker( m_mutex );
      int v = m_value;

         return v;
      }
      

      @

      1 Reply Last reply
      0
      • S Offline
        S Offline
        stvokr
        wrote on last edited by
        #3

        Ok, this is a solution, but it does not answer my question.
        Perhaps this more a general C++ question.

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          I think return m_value creates a copy of the variable, and the mutex locker goes out of scope later (after the return statement). So your solution stvokr should work fine (and is less to type than what topse proposed).

          (Z(:^

          1 Reply Last reply
          0
          • C Offline
            C Offline
            ckakman
            wrote on last edited by
            #5

            "Here":http://stackoverflow.com/questions/25865210/c-function-end-and-local-destruction-order is the same question, albeit returning a string, on Stackoverflow.

            The local variables are destructor in the reverse order they are created on the stack. The returned value is either a temporary copy or constructor in place where it is assigned, again a copy in case of an int. Either way, QMutexLocker object, which is created before the temporary, gets destructed last.

            By the way there is a bug in your sample code. Since your QMutexLocker is not a named variable, it gets destructed at the ';' on the line its created. You need to name it so that it survives the function scope.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              stvokr
              wrote on last edited by
              #6

              Yes, of course it is this way.

              @int getValue()
              {
              QMutexLocker locker( m_mutex );
              return m_value;
              }@

              I think this version works.

              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