Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Wait until condition is not met - but why mutex?



  • Hello,
    I have 2 threads. I would like something like:
    main Thread wait when special condition will be met ( for example variable "a" will be 6 )
    the second Thread do something and set variable "a"

    mainThread:
    another operations
    waitUntilVariableAIsNot6
    another operations
    
    second Thread:
    another operations
    a = 6;
    another operations
    

    I find very good class for that: QWaitCondition
    So I would like to write:

    mainThread:
    another operations
    waitCon.wait();
    another operations
    
    second Thread:
    another operations
    a = 6;
    waitCon.wakeAll();
    another operations
    

    My code is not good. In QWaitCondition we have wait() function, but one of the parameter is "QMutex *lockedMutex". Why I have to use mutex in this example?


  • Lifetime Qt Champion

    Hi,

    Because you want to access a shared resource and thus its access must be protected.



  • @SGaist But I know I use only once variable "a" in second Thread.

    EDIT

    I agree with you. My example is very uncommon. But why we can't have something like:

    mainThread:
    another operations
    waitCon.wait();
    mutex.lock();
    another important operations
    mutex.unlock();
    another operations
    
    second Thread:
    another operations
    mutex.lock();
    a = 6;
    mutex.unlock();
    waitCon.wakeAll();
    another operations
    

    And function wait() can be without parameter "QMutex *lockedMutex".


  • Lifetime Qt Champion

    In that case it's simply used as a mean to stop the thread execution until the wait condition is awaken.

    Depending on what you want to implement, using signals and slot might be a better tool.



  • @SGaist I can't find function like wait() when I want to use signals and slots. Of course in slot I can set variable a.


  • Lifetime Qt Champion

    Because there are none.

    Can you explain what your two threads are doing ? Especially why the first one needs to wait for a to be equal to 6 ?



  • @SGaist I don't have at the moment very good example Sometimes I have code:

    fun()
    {
    fun1();
    fun2();
    fun3();
    }
    
    void timerTimeoutSlot()
    {
    do Something // for example set very important Variable;
    }
    

    I would like to something like:

    fun()
    {
    fun1();
    wait when the very important variable will be set
    fun2();
    fun3();
    }
    

    Because when this variable will not set my fun2() can't be execute.


  • Lifetime Qt Champion

    And fun should be called repeatedly ?



  • @SGaist said in Wait until condition is not met - but why mutex?:

    repeatedly

    When I look at my code I think I can do something like:

    void timerTimeoutSlot()
    {
    do Something // for example set very important Variable;
    fun2();
    fun3();
    }
    

    But Could you tell me what is going on when signal is emitted? I talk about only 1 thread. For example:

    function()
    {
    operation a;
    operation b;
    operation c;
    operation d;
    operation e;
    operation f;
    }
    
    timerTimeoutSlot()
    {
    operation m;
    operation n;
    operation o;
    }
    
    timer2TimeoutSlot()
    {
    operation x;
    operation y;
    operation z;
    }
    

    And when the operation c is execute, signal timeout in timer1 is emitted
    So operations will be execute like:
    a -> b -> c -> m -> n -> o -> d -> e -> f?

    And second question:
    When the operation c is execute, signal timeout in timer1 is emitted and when operation m is execute, signal timeout in timer 2 is emitted. So operation will be execute like:
    a -> b -> c -> m -> x->y->z->n -> o -> d -> e -> f?

    EDIT:
    I hear that can be something like:
    a -> b -> c -> m -> d->n->e->o->f ( in first question )


  • Lifetime Qt Champion

    If single threaded, the slots will be called after the functions are done in order.

    Looks like you are trying to model some sort of behaviour. What would that be ?



  • @SGaist so will be a b c d e f m n o
    Or
    A b c m d n e o f?

    I am a newbie programmer, so my questions are not very smart and I dont have any plans.


  • Lifetime Qt Champion

    Your answers makes it look like you are trying to model some behaviour that could be represented by a state machine rather that complex threading.

    Since you are starting in the programming field, you should rather stay away from the threading paradigm and learn the rest first. Threading is a very complex topic that offers you many way to shoot yourself in the foot from different unrelated angles at different unrelated time with different unrelated side-effects that will want to go after your foot again.



  • @SGaist Ok :) I will learn thread after easier thing. But please answer for my question about operations. Thank you


  • Lifetime Qt Champion

    If timer2 timeout value is greater than timer they will be in order: function -> timerTimeoutSlot -> timer2TimeoutSlot.


Log in to reply