Qml State machine and backend QMutexLocker



  • 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


  • Lifetime Qt Champion

    Hi,

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



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



  • This post is deleted!

  • Lifetime Qt Champion

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.