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. Qml State machine and backend QMutexLocker
Forum Updated to NodeBB v4.3 + New Features

Qml State machine and backend QMutexLocker

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 1.4k 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.
  • C Offline
    C Offline
    chimera123
    wrote on last edited by chimera123
    #1

    Hello everybody,

    I have a question regarding the following predicament:

    Imagine a QML state machine:

    import QtQml.StateMachine 1.0 as DSM
    
    DSM.StateMachine {
    
    running: true
    childState: DSM.QState.ExclusiveStates
    initialState: sInit
    
         DMS.QState {
            id: sInit
            childState: DSM.QState.ParallelStates
    
            DSM.SignalTransition  {
              signal: /*signal from c++ backend  - onSynchronized (see backend below)*/
              targetState: sStarted
            }
    
            DMS.QState {
              id:  sSubInit1
              onEntered: /*increment counter*/
            }
    
            DMS.QState {
              id:  sSubInit2
              onEntered: /*increment counter*/
            }
         }
    
         DMS.QState {
            id: sStarted
            /*etc...*/
         }
    }
    

    And the c++ backend (Q_INVOKABLE) method to increment some counter:

    void Test::incrementCounter()
    {
        Q_D(Test);
    
        QMutexLocker locker(&(d->mutex));
        ++(d->counterValue);
    
        if(d->counterValue >= d->counterSyncValue) 
            emit synchronized();    
    }
    

    What is the behaviour of emitted signal when the mutex is not yet released? I know that the QStateMachine class uses threading capabilities, but does the QML State machine framework behave in the same way? The parallel states shown above in the QML implementation can access the increment method simultaneously, right?
    Is this a safe way to catch a mutex'ed signal from c++ backend? Any suggestions are appreciated...

    Thank you,
    K

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      IIRC, you have to release the mutex before emitting the signal.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      C 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        IIRC, you have to release the mutex before emitting the signal.

        C Offline
        C Offline
        chimera123
        wrote on last edited by chimera123
        #3

        @SGaist said in Qml State machine and backend QMutexLocker:

        Hi,

        IIRC, you have to release the mutex before emitting the signal.

        Hello,

        thank you for the reply.

        So:

        void Test::incrementCounter()
        {
            Q_D(Test);
        
            bool isSynched = false
        
            d->mutex.lock() ;
            ++(d->counterValue);
        
            if(d->counterValue >= d->counterSyncValue) 
                isSynched = true;
            
            d->mutex.unlock() 
        
            if(isSynched) 
              emit synchronized();    
        }
        

        Local variables (bool isSynched) are thread safe due to stack allocation, but does this also apply to the objects that are set by the qml context property? It should, right?

        Regards, K

        C 1 Reply Last reply
        0
        • C chimera123

          @SGaist said in Qml State machine and backend QMutexLocker:

          Hi,

          IIRC, you have to release the mutex before emitting the signal.

          Hello,

          thank you for the reply.

          So:

          void Test::incrementCounter()
          {
              Q_D(Test);
          
              bool isSynched = false
          
              d->mutex.lock() ;
              ++(d->counterValue);
          
              if(d->counterValue >= d->counterSyncValue) 
                  isSynched = true;
              
              d->mutex.unlock() 
          
              if(isSynched) 
                emit synchronized();    
          }
          

          Local variables (bool isSynched) are thread safe due to stack allocation, but does this also apply to the objects that are set by the qml context property? It should, right?

          Regards, K

          C Offline
          C Offline
          chimera123
          wrote on last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            What do you mean by set "objects that are set by the qml context property" ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            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